简体   繁体   English

如何在LinkedIn中获取访问令牌OAuth 2.0身份验证应该作为POST请求?

[英]How is obtaining the Access Token in LinkedIn OAuth 2.0 authentication supposed to be made as a POST request?

Sorry if this is a simple question. 对不起,如果这是一个简单的问题。

I'm a LinkedIn newbie and am attempting to complete the OAuth 2.0 process to obtain the 60-day "access token" after obtaining the temporary "authorization code" as described at https://developer.linkedin.com/documents/authentication . 我是LinkedIn的新手,并且在获取临时“授权代码”后,我尝试完成OAuth 2.0流程以获取60天的“访问令牌”,如https://developer.linkedin.com/documents/authentication中所述

I had no problems with part 3a - obtaining the temporary authorization code. 我对第3a部分没有任何问题 - 获得临时授权码。 I simply redirected the user to the LinkedIn URL: 我只是将用户重定向到LinkedIn URL:

https://www.linkedin.com/uas/oauth2/authorization?response_type=code
                                       &client_id=YOUR_API_KEY
                                       &scope=SCOPE
                                       &state=STATE
                                       &redirect_uri=YOUR_REDIRECT_URI

and after the user granted permission they are redirected back to my app, and the returned parameters are checked to see if everything went well and the temporary authentication code is retrieved as one of the form parameters returned, as per the docs. 在用户授予权限后,他们会被重定向回我的应用程序,并检查返回的参数以查看是否一切顺利,并根据文档检索临时身份验证代码作为返回的表单参数之一。

However, I am confused as to how to proceed with step 3b - exchanging the authorization code for an access token (which can then be used to make API requests). 但是,我对如何继续执行步骤3b感到困惑 - 交换访问令牌的授权代码(然后可以用来发出API请求)。 My confusion is because even the docs show show what appears to be a typical GET URL with parameters, it says "POST" next to it. 我的困惑是因为即使是文档显示看起来似乎是带参数的典型GET URL,它旁边也会显示“POST”。 So I believe a POST request must be made instead of a GET. 所以我认为必须发出POST请求而不是GET。

But what do they mean by this? 但他们的意思是什么呢? Why do the docs give a URL with query parameters rather than detailing the needed POST request, with a URL and how the parameters should be formatted in the body? 为什么文档提供带有查询参数的URL,而不是详细说明所需的POST请求,URL以及如何在体内格式化参数? I'm basically just not sure how I should form my request for step 3b. 我基本上不确定如何形成我对步骤3b的请求。

I'm assuming I can't simply redirect my users to the 3b URL as I did with the 3a URL, right? 我假设我不能简单地将用户重定向到3b URL,就像我使用3a URL一样,对吧? That would be easy if I could. 如果可以的话,这将很容易。

The language I'm using is server-side JavaScript. 我使用的语言是服务器端JavaScript。 I can redirect. 我可以重定向。 I can form and make POST requests if I know the needed format. 如果我知道所需的格式,我可以形成并发出POST请求。 I can create GET requests. 我可以创建GET请求。 I'm just not familiar with this situation, where the docs which say the request should look like this: 我只是不熟悉这种情况,其中说请求的文档应如下所示:

https://www.linkedin.com/uas/oauth2/accessToken?grant_type=authorization_code
                                       &code=AUTHORIZATION_CODE
                                       &redirect_uri=YOUR_REDIRECT_URI
                                       &client_id=YOUR_API_KEY
                                       &client_secret=YOUR_SECRET_KEY

yet at the same time indicate that it is a POST request. 但同时表明它是一个POST请求。

Is there some short-cut method of using that URL as is to make a POST request? 是否有一些使用该URL的快捷方法来发出POST请求? Or am I supposed to understand how to take that URL and turn it into a typical post request and put all the parameters into the body and just make my request to https://www.linkedin.com/uas/oauth2/accessToken itself? 或者我是否应该了解如何获取该URL并将其转换为典型的帖子请求并将所有参数放入正文并将我的请求发送到https://www.linkedin.com/uas/oauth2/accessToken本身?

Sorry if it's something that should be obvious. 对不起,如果这是显而易见的事情。 It's just not something I have encountered before. 这不是我以前遇到的事情。

Any help here would be appreciated. 任何帮助在这里将不胜感激。

Thanks, 谢谢,

doug 道格

Consider as a tutorial and it will smoothly drive you to the access token :) 考虑作为教程,它将顺利地驱动您访问令牌:)

Step 1: 第1步:

After you have created the app: 创建应用程序后: 第1步

write this as I will follow with the same 写下这个,因为我将遵循相同的
I had created a folder "/var/www/html/code" 我创建了一个文件夹“/ var / www / html / code”

Step 2: 第2步:

call: 呼叫:

https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id= enter your client_id &redirect_uri=http%3A%2F%2Flocalhost%2Fcode&state=987654321&scope=r_basicprofile https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id= 输入您的client_id &redirect_uri = http%3A%2F%2Flocalhost%2Fcode&state = 987654321&scope = r_basicprofile

reply: 回复:

http://localhost/code/?code=AQTnRRDM_pMw6Nn8jutiCKTH3xmqHOPnVb4udfGI7KbK7mLhie2XqcEZf1IOycVdgGC5mamWEiFd3DxJznxJZYaix_UCGlIH_PbJJZG720LBk5heSrE&state=987654321 HTTP://本地主机/代码/代码= AQTnRRDM_pMw6Nn8jutiCKTH3xmqHOPnVb4udfGI7KbK7mLhie2XqcEZf1IOycVdgGC5mamWEiFd3DxJznxJZYaix_UCGlIH_PbJJZG720LBk5heSrE&状态= 987654321

You will get different reply but structure will be same. 你会得到不同的答案,但结构将是相同的。

Step 3: 第3步:

curl -X POST --http1.1 " https://www.linkedin.com/oauth/v2/accessToken " --cookie "X-Csrf-Token: 987654321" -d "grant_type=authorization_code&code=AQTnRRDM_pMw6Nn8jutiCKTH3xmqHOPnVb4udfGI7KbK7mLhie2XqcEZf1IOycVdgGC5mamWEiFd3DxJznxJZYaix_UCGlIH_PbJJZG720LBk5heSrE&redirect_uri=http%3A%2F%2Flocalhost%2Fcode&client_id= client_id &client_secret= client_secret " -H "Content-Type: application/x-www-form-urlencoded" 卷曲-X POST --http1.1 “ https://www.linkedin.com/oauth/v2/accessToken ” --cookie “X-CSRF令牌:987654321” -d“grant_type = authorization_code&代码= AQTnRRDM_pMw6Nn8jutiCKTH3xmqHOPnVb4udfGI7KbK7mLhie2XqcEZf1IOycVdgGC5mamWEiFd3DxJznxJZYaix_UCGlIH_PbJJZG720LBk5heSrE&REDIRECT_URI = HTTP%3A %2F%2Flocalhost%2Fcode&client_id = client_id &client_secret = client_secret “-H”Content-Type:application / x-www-form-urlencoded“

When following the spec you should send the parameters in a POST with Content-Type set to application/x-www-form-urlencoded , so regular form post but it turns out that LinkedIn also allows to exchange the code for a token by using a plain GET with the parameters in the query part of the URL as your sample shows. 当遵循规范时,您应该在POST中将参数发送到Content-Type设置为application/x-www-form-urlencoded ,这样常规表单帖子但是事实证明LinkedIn也允许通过使用如示例所示,使用URL的查询部分中的参数进行简单GET。 It is not recommended (and in violation of the spec) to use GET as parameters end up on logs, browser history and the GET is more vulnerable to hijacking attacks. 不建议(并且违反规范)使用GET作为参数最终日志,浏览器历史记录和GET更容易受到劫持攻击。

Here's a (edited) CURL trace of a GET to LinkedIn: 这是一个(编辑过的)GET到LinkedIn的CURL跟踪:

* Connected to www.linkedin.com (108.174.2.129) port 443 (#0)
...
> GET /uas/oauth2/accessToken?grant_type=authorization_code&code=<code>&redirect_uri=<url>&client_id=<id>&client_secret=<key> HTTP/1.1
> User-Agent: curl/7.39.0
> Host: www.linkedin.com
> Accept: */*
> 
< HTTP/1.1 200 OK
< Server: Apache-Coyote/1.1
< P3P: CP="CAO CUR ADM DEV PSA PSD OUR"
< Content-Type: application/json;charset=UTF-8
< Content-Language: en-US
< Content-Length: 219
< Vary: Accept-Encoding
< Date: Wed, 31 Dec 2014 11:04:55 GMT
< X-FS-UUID: 5b7c888b35f0b413d05936cac02a0000
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
< X-Li-Fabric: PROD-ELA4
< Strict-Transport-Security: max-age=0
...
< 
* Connection #0 to host www.linkedin.com left intact
{"access_token":"<>","expires_in":5178866}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM