1

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

我正在关注这个 URL 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"}

有任何想法吗?提前致谢。

4

3 回答 3

0

I got the same error as you. I also met the following conditions:

  • My request was a POST request.
  • My redirect_uri's were the same in /authorization and /accessToken calls.
  • The /accessToken call was executed immediately after receiving the authorization code, so it wouldn't expire.

What finally did the trick for me was revoking the access token generated on the application details page on https://www.linkedin.com/secure/developer.

This is an access token for oAuth 1.a and is not compatible with oAuth 2.0 on which the linkedIn api is currently running.
After revoking this access token I was able to get a new one with the /authorization and /accessToken calls.

于 2014-03-10T13:59:19.047 回答
0

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

于 2014-01-30T09:16:25.953 回答
0

我看到这是一个较旧的线程,但是如果它可以帮助任何人,这是我的工作解决方案,截至 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;
}

希望它可以帮助某人:)

于 2018-12-27T09:40:35.050 回答