OnePost

FREEMIUM
By Adam Darrah | Updated एक महीने पहले | Social
Popularity

8.5 / 10

Latency

954ms

Service Level

100%

Health Check

N/A

Back to All Tutorials (10)

Sign in with Twitter

Before starting this tutorial, make sure you have added a Twitter Provider to your OnePost API.

Get the Required Provider ID

First, we’ll use the Get All Providers API endpoint to find our Twitter Provider ID. We’ll need this in order to get our link to sign in.

curl --request GET \
> --url 'https://onepost1.p.rapidapi.com/api/v1/providers?secret_key=sk-7a0...' \
> --header 'x-rapidapi-host: onepost1.p.rapidapi.com' \
> --header 'x-rapidapi-key: 9d0...'

{"current_page":1,"per_page":30,"total_entries":1,"collection":[{"id":7,"type":"Providers::Twitter","api_key":"L5oN097313NwB8StifbIpmDQT","created_at":"2021-02-19T16:50:15.858-05:00","updated_at":"2021-02-19T16:50:15.858-05:00","callback_url":null,"api_secret_key":"1pKXkMH2YKYA3UBLjepCrq3cr4GVKPYcATxpPeIqi5U0olJQU9"}]}

Let’s note that our Twitter Provider has an ID of 7 in OnePost.

Create the Authorization

Now that we have the Provider ID, we’ll use the Create an Authorization API endpoint to assist in creating our Authorization. An Authorization in OnePost is a model representing a user’s permission for your application to use their account. This record is created when they sign into the Provider (such as Twitter or Facebook) to grant access through their web browser. Let’s use the API to get the URL we’ll need:

curl --request POST \
> --url 'https://onepost1.p.rapidapi.com/api/v1/authorizations?secret_key=sk-7a0...' \
> --header 'content-type: application/json' \
> --header 'x-rapidapi-host: onepost1.p.rapidapi.com' \
> --header 'x-rapidapi-key: 9d0...' \
> --data '{
>     "authorization": {
>         "provider_id": 7
>     }
> }'

{"instructions":["Perform the following steps to complete the authorization process:","  1. In your web browser, navigate to the provided URL.","  2. Sign in to Twitter.","  3. You will be redirected back to the original application."],"url":"https://api.getonepost.com/users/auth/twitter?provider_id=7&public_key=pk-7a0..."}

You can see that when we call this endpoint, we get back a unique URL (in a Production environment you would simply have a link from your website with this URL that the user can click on). Let’s copy and paste this into our web browser to try it out.

Notice that once you sign in you are redirected back to OnePost. You are able to specify where the user should be redirected with the callback_url field on the Provider record. (Note: Look for the X-OnePost-Authorization-Id, X-OnePost-Provider-Id, and X-OnePost-Public-Key header values in the request sent to your callback_url)

Once the user has signed into Twitter, you can call the Get All Authorizations endpoint to see the new Authorization record.

curl --request GET \
> --url 'https://onepost1.p.rapidapi.com/api/v1/authorizations?secret_key=sk-7a0...' \
> --header 'x-rapidapi-host: onepost1.p.rapidapi.com' \
> --header 'x-rapidapi-key: 9d0...'

{"current_page":1,"per_page":30,"total_entries":1,"collection":[{"id":6,"created_at":"2021-02-19T17:44:46.828-05:00","updated_at":"2021-02-19T17:44:46.828-05:00","provider_id":7,"type":"Authorizations::Twitter","consumer_key":"815...","consumer_secret":"HLh...","authorized_pages":[{"id":7,"authorization_id":6,"name":"1bertlol","service_id":"81534513","type":"AuthorizedPages::Twitter","info":{"id":81534513,"url":"https://t.co/23KYmkQZJB","lang":null,"name":"Adam Darrah","id_str":"81534513","entities":{"url":{"urls":[{"url":"https://t.co/23KYmkQZJB","indices":[0,23],"display_url":"dropkiq.com","expanded_url":"https://www.dropkiq.com/"}]},"description":{"urls":[]}},"location":"Keystone, Indiana","verified":false,"following":false,"protected":false,"suspended":false,"time_zone":null,"created_at":"Sun Oct 11 06:36:24 +0000 2009","utc_offset":null,"description":"I like futurama, websites, and code. Founder of Dropkiq ?","geo_enabled":false,"screen_name":"1bertlol","listed_count":4,"friends_count":266,"is_translator":false,"notifications":false,"statuses_count":1693,"default_profile":false,"followers_count":175,"translator_type":"none","favourites_count":771,"profile_image_url":"http://pbs.twimg.com/profile_images/1266163001532518400/K41HOU0Y_normal.jpg","profile_link_color":"0084B4","profile_text_color":"333333","follow_request_sent":false,"contributors_enabled":false,"has_extended_profile":false,"default_profile_image":false,"is_translation_enabled":false,"profile_background_tile":true,"profile_image_url_https":"https://pbs.twimg.com/profile_images/1266163001532518400/K41HOU0Y_normal.jpg","needs_phone_verification":false,"profile_background_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_background_image_url":"http://abs.twimg.com/images/themes/theme18/bg.gif","profile_sidebar_border_color":"000000","profile_use_background_image":true,"profile_background_image_url_https":"https://abs.twimg.com/images/themes/theme18/bg.gif"},"created_at":"2021-02-19T17:44:46.869-05:00","updated_at":"2021-02-19T17:44:46.869-05:00"}]}]}

After an Authorization has been created, we will automatically create AuthorizedPage records for the Authorization. An AuthorizedPage represents a social page that you’re allowed to post to via the Authorization. For Twitter, there will be a single AuthorizedPage (their Twitter feed). Let’s use the Get All Authorized Pages endpoint to see our new Authorized Page record.

curl --request GET \
> --url 'https://onepost1.p.rapidapi.com/api/v1/authorized_pages?secret_key=sk-7a0...' \
> --header 'x-rapidapi-host: onepost1.p.rapidapi.com' \
> --header 'x-rapidapi-key: 9d0...'

{"current_page":1,"per_page":30,"total_entries":1,"collection":[{"id":7,"authorization_id":6,"name":"1bertlol","service_id":"81534513","type":"AuthorizedPages::Twitter","info":{"id":81534513,"url":"https://t.co/23KYmkQZJB","lang":null,"name":"Adam Darrah","id_str":"81534513","entities":{"url":{"urls":[{"url":"https://t.co/23KYmkQZJB","indices":[0,23],"display_url":"dropkiq.com","expanded_url":"https://www.dropkiq.com/"}]},"description":{"urls":[]}},"location":"Keystone, Indiana","verified":false,"following":false,"protected":false,"suspended":false,"time_zone":null,"created_at":"Sun Oct 11 06:36:24 +0000 2009","utc_offset":null,"description":"I like futurama, websites, and code. Founder of Dropkiq ?","geo_enabled":false,"screen_name":"1bertlol","listed_count":4,"friends_count":266,"is_translator":false,"notifications":false,"statuses_count":1693,"default_profile":false,"followers_count":175,"translator_type":"none","favourites_count":771,"profile_image_url":"http://pbs.twimg.com/profile_images/1266163001532518400/K41HOU0Y_normal.jpg","profile_link_color":"0084B4","profile_text_color":"333333","follow_request_sent":false,"contributors_enabled":false,"has_extended_profile":false,"default_profile_image":false,"is_translation_enabled":false,"profile_background_tile":true,"profile_image_url_https":"https://pbs.twimg.com/profile_images/1266163001532518400/K41HOU0Y_normal.jpg","needs_phone_verification":false,"profile_background_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_background_image_url":"http://abs.twimg.com/images/themes/theme18/bg.gif","profile_sidebar_border_color":"000000","profile_use_background_image":true,"profile_background_image_url_https":"https://abs.twimg.com/images/themes/theme18/bg.gif"},"created_at":"2021-02-19T17:44:46.869-05:00","updated_at":"2021-02-19T17:44:46.869-05:00"}]}

Now that we have an AuthorizedPage record with an ID of 7, we are ready to make a Post!