简体   繁体   English

如何使用授权码获取访问令牌?

[英]How to get access token using authorization code?

I am trying to develop an app which requires gmail authorization and authentication. 我正在尝试开发需要gmail授权和身份验证的应用。 using oAuth2 i have retrieved the authorization code but now how can i get the access token using this code...?? 使用oAuth2,我已经检索了授权代码,但是现在如何使用此代码获取访问令牌...? I am using javascript and html5 for this app. 我正在为此应用程序使用javascript和html5。

We need to send Post request which will include all the parameters like : client_secret, client_id, grant_type and code. 我们需要发送Post请求,其中将包含所有参数,例如:client_secret,client_id,grant_type和代码。 Since my app is Windows 8 based,so before sending POST request you need to encode your parameters otherwise you wont get the token. 由于我的应用程序基于Windows 8,因此在发送POST请求之前,您需要对参数进行编码,否则您将无法获得令牌。

Hi all i have got answer to my question. 大家好,我已经回答了我的问题。

After getting success code, you need to Use encodeURIComponent to encode each and every field in the query and then send the post request. 获取成功代码后,您需要使用encodeURIComponent对查询中的每个字段进行编码,然后发送发布请求。

eg - var post_data = 'client_id=' + encodeURIComponent(CLIENTID) + '&redirect_uri=' + encodeURIComponent(REDIRECT) + '&code=' + encodeURIComponent(CODE) + '&grant_type=' + encodeURIComponent(GRANT_TYPE) + '&client_secret=' + encodeURIComponent(CLIENT_SECRET); 例如-var post_data ='client_id ='+ encodeURIComponent(CLIENTID)+'&redirect_uri ='+ encodeURIComponent(REDIRECT)+'&code ='+ encodeURIComponent(CODE)+'&grant_type ='+ encodeURIComponent(GRANT_TYPE)+'&client_secret ='+ encodeURIComponent(CLIENT_SECRET);

and u need to assign this to the data field in your post request. 您需要将其分配给您的发帖请求中的数据字段。

Then only , you will be able to get the access_token. 然后只有,您将能够获得access_token。 I have used this code in my metro app and now i am able to successfully log into my gmail account via my application. 我已经在Metro应用中使用了此代码,现在我可以通过我的应用成功登录到Gmail帐户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用从客户端收到的Google授权代码在服务器端javascript(nodejs)上获取访问令牌 - Get access token on server side javascript (nodejs) using google authorization code received from client side 如何以编程方式(使用JS / PHP)为github用户获取curl“授权令牌(OAuth令牌)”? - How to get curl “Authorization token(OAuth token)” programmatically (using JS/PHP ) for github user? 如何使用 puppeteer 获取 oauth 访问令牌? - How to get oauth access token using puppeteer? 使用Node中的googleapis包交换访问令牌的授权码 - Exchanging authorization code for access token with googleapis package in Node 使用 GM_xmlhttpRequest 交换访问令牌的 Quire 授权代码 - Exchange Quire authorization code for access token with GM_xmlhttpRequest 获取 pinterest 访问令牌的代码 - Code to get access token for pinterest 如何使用Angular JS使用Authorization Bearer令牌从服务器获取图像 - How to get an Image from the Server using Authorization Bearer token using Angular JS 从授权码google oauth2获取刷新令牌 - get refresh token from authorization code google oauth2 如何在 websocket javascript 客户端中传递授权承载访问令牌 - how to pass Authorization Bearer access token in websocket javascript client 如何处理授权令牌 - How to handle authorization token
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM