无法获取领英OAuth访问令牌

8

我知道有些人会评论说这篇文章是许多问题的重复,但我已经尝试了许多方法来实现领英Oauth中的访问令牌。解释一下我尝试过的。

1)我正在遵循官方文档Linkedin Oauth2

2)我成功地从第2步获取了授权代码,并将该代码传递给第3步以交换授权代码以获取访问令牌。但我收到以下错误 {"error_description":"missing required parameters, includes an invalid parameter value, parameter more than once. : Unable to retrieve access token : appId or redirect uri does not match authorization code or authorization code expired","error":"invalid_request"}

3)根据一些链接,我需要在标题中设置内容类型。 Link which tells to set content-type is missing

4)然后我尝试调用https://www.linkedin.com/uas/oauth2/accessToken这个服务,而不是POStGET。并将数据作为查询参数传递。

5) 有些链接说oauth代码在20秒后过期,所以我检查了一下,我在不到1秒的时间内调用了访问令牌。

6) 如果我像下面这样传递数据并使用url作为https://www.linkedin.com/uas/oauth2/accessToken

var postData = {
                grant_type: "authorization_code",
                code: authCode,
                redirect_uri: 'https%3A%2F%2Foauthtest-mydeployed-app-url',
                client_id: 'my_client_id',
                client_secret: 'secret_key'
            };

7) 使用Get调用我的URL,我尝试了以下代码:https://www.linkedin.com/uas/oauth2/accessToken?grant_type=authorization_code&code='+authCode+'&redirect_uri=https%3A%2F%2Foauthtest-mydeployed-app-url&client_id=my_client_id&client_secret=secret_key

即使状态代码为200,我仍然收到错误消息(使用GET api),如果通过在正文中传递postData来进行POSt,则会收到错误的请求400状态代码。

不明白为什么没有获取访问代码。我已经阅读了很多解决方案。如要求,现在分享代码。

sap.ui.define([
 "sap/ui/core/mvc/Controller",
 "sap/m/MessageToast"
], function (Controller, MessageToast) {
 "use strict";

 return Controller.extend("OauthTest.OauthTest.controller.View1", {
  onPress: function (evt) {
   var sPath =
    'https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=my_client_id&redirect_uri=https%3A%2F%2Foauthtest-mydeployed-app-url&state=DCEeFWf45A53sdfKef424&scope=r_basicprofile';
   window.location.href = sPath;
            var oRouter = new sap.ui.core.UIComponent.getRouterFor(this);
   oRouter.navTo("View2", {
    "username": "Test"
   });
   MessageToast.show(evt.getSource().getId() + " Pressed");
  },
  
  //after user allows access, user will be redirected to this app with code and state in URL
  //i'm fetching code from URL in below method(call is happening in max.569ms)
  
  onAfterRendering: function () {
   var currentUrl = window.location.href;
   var url = new URL(currentUrl);
   var authCode = url.searchParams.get("code");
   if (authCode !== undefined && authCode !== null) {
    var postData = {
     grant_type: "authorization_code",
     code: authCode,
     redirect_uri: 'https%3A%2F%2Foauthtest-mydeployed-app-url',
     client_id: 'my_client_id',
     client_secret: 'secret_key'
    };
    
   /* var accessTokenUrl = 'https://www.linkedin.com/uas/oauth2/accessToken?grant_type=authorization_code&code=' + authCode +'&redirect_uri=https%3A%2F%2Foauthtest-mydeployed-app-url&client_id=my_client_id&client_secret=secret_key';*/

    var accessTokenUrl = 'https://www.linkedin.com/uas/oauth2/accessToken';

    $.ajax({
     url: accessTokenUrl,
     type: "POST",
     beforeSend: function (xhr) {
      xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
     },
     data: postData,
     success: function (data, textStatus, jqXHR) {
      console.log(data);
      alert('success');
     },
     error: function (jqXHR, textStatus, errorThrown) {
      console.log(errorThrown);
      alert('error');
     }
    });
   }
  }

 });
});

需要帮助,感激不尽..!!!


如错误所述,您需要检查以下事项: 1)确保访问代码未过期。 2)生成访问令牌的URL为:https://www.linkedin.com/oauth/v2/accessToken 3)方法:POST 4)POSTFIELDS如下: i)grant_type ='authorization_code' ii)code="<ur access code>" iii)redirect_uri=<exacttly same URL were access code is generated> iv)client_id=<LINKEDIN_CLIENT_ID> v)client_secret=<LINKEDIN_CLIENT_SECRET> - Ketav
以下是 PHP 的示例代码,供您参考。$ch = curl_init(); curl_setopt($ch, CURLOPT_URL,"https://www.linkedin.com/oauth/v2/accessToken"); curl_setopt($ch, CURLOPT_POST, 0); curl_setopt($ch, CURLOPT_POSTFIELDS,"grant_type=authorization_code&code=".$code."&redirect_uri=".env('LINKEDIN_REDIRECT_URL')."&client_id=".env('LINKEDIN_CLIENT_ID')."&client_secret=".env('LINKEDIN_CLIENT_SECRET')); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $server_output = curl_exec ($ch); curl_close ($ch); - Ketav
@KetavChotaliya,我按照您的第一条评论尝试了一下POST请求,但是这种情况下我得到了错误请求和状态码400。我们还需要在头部传递content-type。我检查了我的访问代码并没有过期。 - Prasanna
需要仔细调试...流程与我上面提到的相同。如果可能,请分享您的代码。 - Ketav
@KetavChotaliya增加了代码以提高可读性。 - Prasanna
1个回答

4

经过长时间的搜索,我终于很高兴能够发布我的答案。 我所做的每一步都是正确的,但有一件事我在这里遗漏了,就是Linkedin API不支持CORS。

我尝试实现Javascript SDK,它运行得非常好。但API却不行。 然后我找到了一个非常有用的链接,它说我需要通过允许CORS从后端实现Rest API,而不是从前端实现。

请确保遵循我在帖子中提到的所有要点。 并且对于允许CORS,请参阅此链接。您将获得数据,但仅限于LinkedIn条款基本资料可访问的用户个人资料。

希望这篇文章可以帮助一些人节省更多的搜索时间。


我要说的第一件事是,由于CORS yap上的浏览器保护,javascript对API的调用可能会失败。 无论如何,我这边的问题只是一个时间问题。https://dev59.com/Xl4c5IYBdhLWcg3wSIrf#28220525?noredirect=1#comment79610296_28220525 - mwm

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接