【问题标题】:OAuth: HTTP 401 Status when trying to get access token to LinkedInOAuth:尝试获取 LinkedIn 访问令牌时的 HTTP 401 状态
【发布时间】:2012-03-28 11:57:22
【问题描述】:

使用linkedin-j,我的应用程序的一部分中有以下代码

LinkedInOAuthService service = LinkedInOAuthServiceFactory.getInstance()
        .createLinkedInOAuthService(consumerKey, consumerSecret);
LinkedInRequestToken requestToken = 
                    service.getOAuthRequestToken(linkedinCallbackURL);
String authUrl = requestToken.getAuthorizationUrl(); 

我重定向到authUrl 指向的页面,然后转到我授权我的应用程序的正确LinkedIn 页面。然后,生成linkedinCallbackURL指向的页面的代码将执行:

String verifier = request.getParameter("oauth_verifier");
LinkedInOAuthService oauthService = 
    LinkedInOAuthServiceFactory.getInstance()
                  .createLinkedInOAuthService(consumerKey, consumerSecret);  

LinkedInRequestToken requestToken = oauthService.getOAuthRequestToken();  
LinkedInAccessToken accessToken = oauthService
            .getOAuthAccessToken(requestToken, verifier);

很遗憾,我收到了这个错误:

com.google.code.linkedinapi.client.oauth.LinkedInOAuthServiceException:oauth.signpost.exception.OAuthCommunicationException:与服务提供商通信失败:服务器返回 HTTP 响应代码:401 用于 URL:https://api.linkedin.com/uas/oauth/accessToken

原因:oauth.signpost.exception.OAuthCommunicationException:与服务提供者通信失败:服务器返回 HTTP 响应代码:401 for URL:https://api.linkedin.com/uas/oauth/accessToken

有什么问题?

【问题讨论】:

    标签: java oauth linkedin linkedin-j


    【解决方案1】:

    事实证明,问题是我必须在两个点上都使用相同的 LinkedInRequestToken 对象。为此,我将其保存在第一部分的会话中:

    LinkedInRequestToken requestToken = 
                service.getOAuthRequestToken(linkedinCallbackURL);
    session.setAttribute("requestToken", requestToken); // <== THE BEEF
    String authUrl = requestToken.getAuthorizationUrl();
    

    然后我从会话中检索它:

    LinkedInOAuthService oauthService = LinkedInOAuthServiceFactory.getInstance()
            .createLinkedInOAuthService(consumerKey, consumerSecret);  
    // LinkedInRequestToken requestToken = oauthService.getOAuthRequestToken();
    LinkedInRequestToken requestToken = 
            (LinkedInRequestToken) session.getAttribute("requestToken")  
    LinkedInAccessToken accessToken = oauthService
        .getOAuthAccessToken(requestToken, verifier);
    

    注意:发布问题和答案是因为我搜索了很多,但没有人发现这个特定问题。本着this question on Meta 的精神做这件事。另外,我没有回答一些老问题,因为发生此错误的原因有很多(就像LinkedIn API 的NullPointerException...),而且我发现的问题与我的原因不同见过。

    【讨论】:

    • +1。我遇到了类似的问题,您的工作为我指明了正确的方向。就我而言,我使用的是 WebAPI,所以我没有使用会话,而是使用 cookie,并且在读取数据时无意中反转了秘密/值。感谢分享!
    • Exception in thread "main" com.google.code.linkedinapi.client.oauth.LinkedInOAuthServiceException: oauth.signpost.exception.OAuthCommunicationException: Communication with the service provider failed: Server returned HTTP response code: 401 for URL: https://api.linkedin.com/uas/oauth/requestToken 出现错误,我该如何解决?
    • 感谢您对自己问题的回复!我正在使用 Liferay,并在我的 MVCPortlet 类中声明了一个保留令牌的私有字段。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-09
    相关资源
    最近更新 更多