无法获取访问令牌 linkedin Oauth

Unable to get access Token linkedin Oauth

我知道有些人会发表这样的评论 post 是很多问题的重复,但我已经尝试了很多方法来在 linkedin 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) 根据一些链接,我需要在 header.

中设置 content-type

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

5) 一些 link 说 oauth 代码在 20 秒后过期,所以我已经检查过,我正在调用访问令牌不到 1 秒。

6) 如果我像下面那样在 Body 参数中传递数据并使用 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) 如果 POSt 通过在 body 中传递 postData 我得到了错误的请求 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');
     }
    });
   }
  }

 });
});

我们会提供帮助..!!!

经过如此多的搜索,我终于很高兴 post 我的答案。 我所做的每一步都是正确的,但我在这里遗漏了一件事,Linkedin API 不支持 CORS。

我尝试实施 Javascript SDK,效果很好。但是 API 不是。 然后我发现非常有用 说我需要通过允许 CORS 从后端而不是从前端实现 Rest API。

确保遵循我在 post 中提到的所有要点。 对于 follow this link. You will get data but only basic profile of user according to LinkedIn Terms data can be accessible

希望这个 post 可以帮助别人有时间搜索更多