【问题标题】:LinkedIn - How to get access token?LinkedIn - 如何获取访问令牌?
【发布时间】:2018-12-27 17:40:35
【问题描述】:

我正在尝试从 LinkedIn 获取访问令牌。

我正在关注这个网址https://developer.linkedin.com/documents/authentication

我可以获得授权码。

但是当我将授权码传递给这个 URL 时

 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

我收到以下格式的错误

{"error":"invalid_request","error_description":"missing required parameters, includes an invalid parameter value, parameter more then once. : Unable to retrieve access token : appId or redirect uri does not match authorization code or authorization code expired"}

有什么想法吗?提前致谢。

【问题讨论】:

  • 这个错误似乎不言自明 - 您的重定向 URI 与授权代码不匹配或代码已过期。这两项你检查了吗?
  • 感谢您的回复。我检查了这两个项目。

标签: c# linkedin


【解决方案1】:

这是因为授权码在 20 秒后过期。因此,您必须在该时间范围内获得访问令牌。

【讨论】:

    【解决方案2】:

    我遇到了和你一样的错误。我还满足了以下条件:

    • 我的请求是 POST 请求。
    • 我的redirect_uri/authorization/accessToken 通话中的相同。
    • /accessToken 调用在收到授权码后立即执行,所以 它不会过期。

    最终对我有用的是撤销在https://www.linkedin.com/secure/developer 的应用程序详细信息页面上生成的访问令牌。

    这是 oAuth 1.a 的访问令牌,与当前运行 linkedIn api 的 oAuth 2.0 不兼容。
    撤销此访问令牌后,我可以通过 /authorization/accessToken 调用获得一个新令牌。

    【讨论】:

      【解决方案3】:

      我看到这是一个较旧的线程,但是如果它可以帮助任何人,这是我的工作解决方案,截至 2018 年 12 月,我正在使用 MVC 核心 2.0:

      首先,像这样重定向到LinkedIn

      var url = "https://" + Request.Host + "/Login/LoginLinkedIn";            
      url = WebUtility.UrlEncode(url);    
      var redirectLinkedIn = "https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id=*ClientId*&client_secret=*ClientSecret*&redirect_uri=" + url + "&state=*random required nummeric value*";            
      return Redirect(redirectLinkedIn);
      

      之后,您将在 Login/LoginLinkedIn 操作中收到答案(不要忘记在您的应用设置授权重定向 URL 中指定此路径)。

      在那里你将使用这个私有方法来获取一个充满用户数据的动态对象

      private dynamic GetLinkedInUser(string code)
      {
          dynamic jresult;
          NameValueCollection parameters = new NameValueCollection {
              {"client_id", *ClientId*},
              {"client_secret", *ClientSecret*},
              {"grant_type", "authorization_code"},
              {"redirect_uri", "https://" + Request.Host + "/Login/LoginLinkedIn"},
              {"code", code}
          };
          WebClient client = new WebClient();
          byte[] result = client.UploadValues("https://www.linkedin.com/oauth/v2/accessToken", "POST", parameters);
          string response = System.Text.Encoding.Default.GetString(result);
          string accessToken = JsonConvert.DeserializeObject<dynamic>(response).access_token;
      
          WebRequest webReq = WebRequest.Create("https://api.linkedin.com/v1/people/~:(id,email-address,first-name,last-name)?format=json");
          webReq.Method = "GET";
          webReq.Headers.Add("Authorization","Bearer "+accessToken);
          HttpWebResponse webResponse = (HttpWebResponse)webReq.GetResponse();
          using (StreamReader reader = new StreamReader(webResponse.GetResponseStream())) {
              string objText = reader.ReadToEnd();
              jresult = JsonConvert.DeserializeObject<dynamic>(objText);
          }
          return jresult;
      }
      

      希望它可以帮助某人:)

      【讨论】:

        猜你喜欢
        • 2012-04-09
        • 1970-01-01
        • 2018-09-26
        • 2021-03-30
        • 1970-01-01
        • 2011-07-11
        • 2017-01-07
        • 2015-10-04
        • 2015-10-11
        相关资源
        最近更新 更多