{"id":21,"date":"2014-04-17T22:18:43","date_gmt":"2014-04-17T22:18:43","guid":{"rendered":"http:\/\/developer-beta.redbooth.com\/?page_id=21"},"modified":"2020-01-09T20:41:45","modified_gmt":"2020-01-09T20:41:45","slug":"authentication","status":"publish","type":"page","link":"https:\/\/redbooth.com\/api\/authentication\/","title":{"rendered":"Authentication"},"content":{"rendered":"<hr \/>\n<p>Apps connect to Redbooth via OAuth2, an open standard followed by most last generation APIs to authenticate and authorise external apps. The following walkthrough will show you how to register an app in Redbooth; how to get the authorisation from a user to retrieve private information from his Redbooth account and how to get an Access Token and a Refresh Token.<\/p>\n<p>Start making API calls in <a style=\"text-decoration: none;\" href=\"#auth-guide\">three easy steps<\/a>!<\/p>\n<p>&nbsp;<\/p>\n<h1 style=\"text-align: justify;\" id=auth-guide><span>Authentication &amp; Authorization Guide<\/span><\/h1>\n<p style=\"text-align: justify;\"><em>Getting started with Redbooth&#8217;s API?  <\/em><em>Follow the steps and start enjoying our platform&#8230;<\/em><\/p>\n<p>&nbsp;<\/p>\n<p><strong>NOTE:<\/strong> OAuth 2 can have different architectures. Redbooth&#8217;s API v3 works with a 3 legged architecture and temporary tokens that require a refresh flow. The three entities involved are: the app, the user and the service provider (Redbooth). Let&#8217;s see how they work together:<\/p>\n<p>&nbsp;<\/p>\n<h3>1. Initial Setup, registering the application<\/h3>\n<p>&nbsp;<\/p>\n<p>Before you can start using OAuth2 with your application, you\u2019ll need to tell Redbooth a bit of information about your application:<\/p>\n<p>&nbsp;<\/p>\n<p>A) Register your application <a style=\"text-decoration: none;\" href=\"https:\/\/redbooth.com\/oauth2\/applications\/new\" target=\"_blank\">here<\/a>:<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/wordpress-production.s3.amazonaws.com\/wp-content\/uploads\/sites\/2\/2014\/04\/Screen-Shot-2015-01-13-at-6.50.16-PM.png\" style=\"text-decoration: none;\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-574 size-large\" src=\"https:\/\/wordpress-production.s3.amazonaws.com\/wp-content\/uploads\/sites\/2\/2014\/04\/Screen-Shot-2015-01-13-at-6.50.16-PM-667x1024.png\" alt=\"Screen Shot 2015-01-13 at 6.50.16 PM\" width=\"667\" height=\"1024\" srcset=\"https:\/\/wordpress-production.s3.amazonaws.com\/wp-content\/uploads\/sites\/2\/2014\/04\/Screen-Shot-2015-01-13-at-6.50.16-PM-667x1024.png 667w, https:\/\/wordpress-production.s3.amazonaws.com\/wp-content\/uploads\/sites\/2\/2014\/04\/Screen-Shot-2015-01-13-at-6.50.16-PM-195x300.png 195w, https:\/\/wordpress-production.s3.amazonaws.com\/wp-content\/uploads\/sites\/2\/2014\/04\/Screen-Shot-2015-01-13-at-6.50.16-PM.png 862w\" sizes=\"auto, (max-width: 667px) 100vw, 667px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Return URI or Callback URL:<\/strong> The redirect URI is the URL within your application that will receive the OAuth2 credentials.<\/p>\n<p>&nbsp;<\/p>\n<p>B) Make a note of both your Api Key and App secret.<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/wordpress-production.s3.amazonaws.com\/wp-content\/uploads\/sites\/2\/2014\/04\/authentication.png\" style=\"text-decoration: none;\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-579 size-large\" src=\"https:\/\/wordpress-production.s3.amazonaws.com\/wp-content\/uploads\/sites\/2\/2014\/04\/authentication-1024x790.png\" alt=\"App credentials\" width=\"1024\" height=\"790\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Now that you have your Client id and Client Secret, this is the flow that you will have to follow in order to get your Access Token and Refresh Token and start making calls to Redbooth&#8217;s API:<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/wordpress-production.s3.amazonaws.com\/wp-content\/uploads\/sites\/2\/2014\/10\/Oauth-1.jpg\" style=\"text-decoration: none;\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-448 size-full\" src=\"https:\/\/wordpress-production.s3.amazonaws.com\/wp-content\/uploads\/sites\/2\/2014\/10\/Oauth-1.jpg\" alt=\"Oauth (1)\" width=\"649\" height=\"609\" srcset=\"https:\/\/wordpress-production.s3.amazonaws.com\/wp-content\/uploads\/sites\/2\/2014\/10\/Oauth-1.jpg 649w, https:\/\/wordpress-production.s3.amazonaws.com\/wp-content\/uploads\/sites\/2\/2014\/10\/Oauth-1-300x281.jpg 300w\" sizes=\"auto, (max-width: 649px) 100vw, 649px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>If this is your first time using OAuth2, we recommend you to take a look at the following article: <a href=\"http:\/\/oauth.net\/about\/\" style=\"text-decoration: none;\">http:\/\/oauth.net\/about\/<\/a>. On the other hand, with Runscope&#8217;s <a href=\"https:\/\/www.runscope.com\/oauth2_tool\" style=\"text-decoration: none;\">Oauth2 Tool<\/a> you will be able to get your Access Token and Refresh Token fast and easily, in an interactive way.<\/p>\n<p>&nbsp;<br \/>\n&nbsp;<\/p>\n<h3>2. Getting the Authorization Code from the User<\/h3>\n<p>&nbsp;<\/p>\n<p>Direct your user to <a href=\"https:\/\/redbooth.com\/oauth2\/authorize\" target=\"_blank\" style=\"text-decoration: none;\">https:\/\/redbooth.com\/oauth2\/authorize<\/a> through either a <strong>POST<\/strong> or a <strong>GET<\/strong> request with the following parameters:<\/p>\n<p><strong>Parameters:<\/strong> For POST, include the parameters in the POST body. For GET, include them as query parameters. In both cases, <strong>URL encode<\/strong> the parameters.<\/p>\n<p>&nbsp;<\/p>\n<div id=\"wrap\" style=\"width: 100%;\">\n<div id=\"left_col\" style=\"float: left; width: 20%; height: 50px;\">response_type<br \/>\n<em>required<\/em><\/div>\n<div id=\"right_col\" style=\"float: left; width: 80%;\">Whether the endpoint returns an Authorization Code. For web applications, a value of <strong>code<\/strong> should be used. For browser-based apps or mobile apps <b>token<\/b> should be used. More information <a href=\"http:\/\/aaronparecki.com\/articles\/2012\/07\/29\/1\/oauth2-simplified\" target=\"_blank\" style=\"text-decoration: none;\">here<\/a>.<\/div>\n<div style=\"clear: both;\"><\/div>\n<div id=\"left_col\" style=\"float: left; width: 20%; height: 50px;\">client_id<br \/>\n<em>required<\/em><\/div>\n<div id=\"right_col\" style=\"float: left; width: 80%;\">The <strong>client_id<\/strong> you obtained in the Initial Setup.<\/div>\n<div style=\"clear: both;\"><\/div>\n<div id=\"left_col\" style=\"float: left; width: 20%; height: 50px;\">redirect_uri<br \/>\n<em>required<\/em><\/div>\n<div id=\"right_col\" style=\"float: left; width: 80%;\">Redbooth will redirect the user back to the url specified in redirect_uri returning an Authorization Code. The <em>redirect_uri<\/em> must have the same hostname and path as the callback url specified in your app settings.<\/div>\n<div style=\"clear: both;\"><\/div>\n<\/div>\n<p>A sample GET request could therefore look like:<br \/>\n<code class=\"toolbar:2 nums:false lang:default decode:true\">GET https:\/\/redbooth.com\/oauth2\/authorize?client_id=YOUR_CLIENT_ID&amp;redirect_uri=http%3A%2F%2Fmysite.com%2Fauth&amp;response_type=code<\/code><br \/>\n&nbsp;<\/p>\n<p><strong>Handling the Response from Redbooth<\/strong><\/p>\n<p>If the user clicked Allow in the previous screen, Redbooth will redirect to the URI you specified earlier with a code parameter. For example, if your redirect URI was https:\/\/www.charle.com\/rose, Redbooth would redirect to:<br \/>\n<code class=\"toolbar:2 nums:false lang:default decode:true\">https:\/\/www.charle.com\/rose?code=123456abcdef<\/code><br \/>\n&nbsp;<\/p>\n<p>However, if the user clicked Deny, you will receive a request with an error and error_description parameter, such as:<br \/>\n<code class=\"toolbar:2 nums:false lang:default decode:true\">GET https:\/\/www.charle.com\/rose?error=access_denied&amp;error_description=The+user+denied+access+to+your+application<\/code><br \/>\n&nbsp;<\/p>\n<p><strong>The following conditions may also cause an error:<\/strong><\/p>\n<div id=\"wrap\" style=\"width: 100%;\">\n<div id=\"left_col\" style=\"float: left; width: 30%; height: 50px;\">invalid_request<\/div>\n<div id=\"right_col\" style=\"float: left; width: 70%;\">The request is missing a required parameter, includes an invalid parameter value, includes a parameter more than once, or is otherwise malformed.<\/div>\n<div style=\"clear: both;\"><\/div>\n<div id=\"left_col\" style=\"float: left; width: 30%; height: 50px;\">unsupported_response_type<\/div>\n<div id=\"right_col\" style=\"float: left; width: 70%;\">\n<p>The authorization server does not support obtaining an authorization code using this method.<\/p>\n<p>Check the value of the <em>code<\/em> param in your request.<\/p>\n<\/div>\n<div style=\"clear: both;\"><\/div>\n<div id=\"left_col\" style=\"float: left; width: 30%; height: 50px;\">access_denied<\/div>\n<div id=\"right_col\" style=\"float: left; width: 70%;\">The resource owner or authorization server denied the request.<\/div>\n<div style=\"clear: both;\"><\/div>\n<div id=\"left_col\" style=\"float: left; width: 30%; height: 50px;\">server_error<\/div>\n<div id=\"right_col\" style=\"float: left; width: 70%;\">The device the user is trying to log in from is not authorized to access the user\u2019s account.<\/div>\n<div style=\"clear: both;\"><\/div>\n<\/div>\n<p>&nbsp;<br \/>\n&nbsp;<\/p>\n<h3>3. Getting the Access Token from Redbooth<\/h3>\n<p>&nbsp;<\/p>\n<p>Once your application has completed the above section and gotten an Authorization Code, it&#8217;ll now need to exchange the Authorization Code for an Access Token from Redbooth. That Access Token will come with a Refresh Token that you will have to store in your app in order to refresh the Access Token when it expires. Each Access Token comes with its unique Refresh Token.<\/p>\n<p>&nbsp;<\/p>\n<p>From your app, request an Access Token:<br \/>\n<code class=\"toolbar:2 nums:false lang:default decode:true\">curl -X POST https:\/\/redbooth.com\/oauth2\/token -d 'client_id=APPLICATION_ID&amp;client_secret=SECRET&amp;code=THE_CODE_YOU_GOT_FROM_REDBOOTH&amp;grant_type=authorization_code&amp;redirect_uri=http%3A%2F%2Fmysite.com%2Faut'<\/code><br \/>\n&nbsp;<\/p>\n<p>If everything goes right and the request is successful, you&#8217;ll receive a 200 response containing a JSON body like this:<\/p>\n<p><code>{\"access_token\":\"1286198b9c618e86fcc7185338jddweet3wdasd342cf26a2bf84d5f7239b12343g\",\"token_type\":\"bearer\",<br \/>\n\"expires_in\":7200,\"refresh_token\":\"c10e7790d130aasd3325e99ea3d123qiwfhfhgc3af93b2ce03506a63c7009992frt\",\"scope\":\"all\"}<\/code><br \/>\n&nbsp;<\/p>\n<p>Use the Access Token when performing requests. If you were, for example, trying to get a user\u2019s account info, a request would look like this:<br \/>\n<code class=\"toolbar:2 nums:false lang:default decode:true\">https:\/\/redbooth.com\/api\/3\/me?access_token=urksAIU8RM0QWPVK7WxhK7RZ24PJ8fVeDSCdXAZG<\/code><\/p>\n<p><strong>NOTE:<\/strong> Tokens expire every 7200 seconds, two hours.<\/p>\n<p>&nbsp;<br \/>\n&nbsp;<\/p>\n<h3>4. Refreshing the Access Token<\/h3>\n<p>&nbsp;<\/p>\n<p>Your Access Token will always expire after 7200 seconds (2 hours), that&#8217;s why it is extremely important to always keep the Refresh Token that Redbooth sends with each Access Token in your app. With that Refresh Token, you will be able to get a new Access Token and its corresponding Refresh Token.<\/p>\n<p>This is the message that you will get if you try to make a call with an expired Access Token:<\/p>\n<p><code><br \/>\n< Status: 401 Unauthorized\n< WWW-Authenticate: Bearer realm=\"Doorkeeper\", error=\"invalid_token\", error_description=\"The access token expired\"\n<\/code><\/p>\n<p>&nbsp;<\/p>\n<p>Make a call like this one and you will get a new Access Token with its corresponding Refresh Token:<\/p>\n<p><code class=\"toolbar:2 nums:false lang:default decode:true\">curl -X POST https:\/\/redbooth.com\/oauth2\/token -d 'client_id=APPLICATION_ID&amp;client_secret=SECRET&amp;refresh_token=REFRESH_TOKEN&amp;grant_type=refresh_token'<\/code><br \/>\n&nbsp;<\/p>\n<p>Here's an example answer:<\/p>\n<p><code>{\"access_token\":\"f5fa837fbde6224c0fad6a029654a9d5a9292e63fcc708204151cf65ddf882de\",\"token_type\":\"bearer\",<br \/>\n\"expires_in\":7200,\"refresh_token\":\"1b2225c55bea5a985aa9d625ff9b89f3246c9ec59d7ae120d261c76fe4b4d886\",\"scope\":\"all\"}<\/code><\/p>\n<p>&nbsp;<\/p>\n<p><strong>NOTE: <\/strong>Take into account that you will have to update the stored Refresh Token every time you ask for a new Access Token, since each Access Token has his unique Refresh Token.<\/p>\n<p>&nbsp;<br \/>\n&nbsp;<\/p>\n<h3>Additional resources<\/h3>\n<p>If you're planning to work with Ruby, Redbooth's Official <a href=\"https:\/\/github.com\/teambox\/redbooth-ruby\" target=\"_blank\" style=\"text-decoration: none;\">Ruby Gem<\/a> will make your life easier! Check how we deal with the authentication process in <a href=\"https:\/\/github.com\/teambox\/\" target=\"_blank\" style=\"text-decoration: none;\">GitHub<\/a>.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Apps connect to Redbooth via OAuth2, an open standard followed by most last generation APIs to authenticate and authorise external apps. The following walkthrough will show you how to register an app in Redbooth; how to get the authorisation from &hellip; <a href=\"https:\/\/redbooth.com\/api\/authentication\/\">Continued<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"open","template":"template-redbooth-pages.php","meta":{"footnotes":""},"class_list":["post-21","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/redbooth.com\/api\/wp-json\/wp\/v2\/pages\/21","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/redbooth.com\/api\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/redbooth.com\/api\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/redbooth.com\/api\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/redbooth.com\/api\/wp-json\/wp\/v2\/comments?post=21"}],"version-history":[{"count":0,"href":"https:\/\/redbooth.com\/api\/wp-json\/wp\/v2\/pages\/21\/revisions"}],"wp:attachment":[{"href":"https:\/\/redbooth.com\/api\/wp-json\/wp\/v2\/media?parent=21"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}