【问题标题】:Linkedin api call using oauth in java在java中使用oauth的Linkedin api调用
【发布时间】:2013-11-22 20:31:37
【问题描述】:

我正在尝试获取 Linkedin 公司更新到我的网站。按照https://developer.linkedin.com/documents/authentication的流程,通过注册生成api key和secret key。

根据documentation,我可以在步骤 a 中生成授权码。 对于步骤 b 生成访问令牌,我使用OAuth in java

request = OAuthClientRequest.tokenLocation("https://www.linkedin.com/uas/oauth2/accessToken")
                        .setGrantType(GrantType.AUTHORIZATION_CODE)
                        .setCode("****")
                        .setRedirectURI("https://www.example.in")
                        .setClientId("*******")
                        .setClientSecret("******").buildBodyMessage();
                    OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
                    GitHubTokenResponse oAuthResponse = oAuthClient.accessToken(request, GitHubTokenResponse.class);
                    String accessToken = oAuthResponse.getAccessToken();
                    String expiresIn = oAuthResponse.getExpiresIn();

但是出现错误

OAuthProblemException{description='Missing parameters: access_token', error='invalid_request', uri='null', state='null', scope='null'}
at org.apache.amber.oauth2.common.exception.OAuthProblemException.error(OAuthProblemException.java:57)
at org.apache.amber.oauth2.common.utils.OAuthUtils.handleOAuthProblemException(OAuthUtils.java:166)
at org.apache.amber.oauth2.common.utils.OAuthUtils.handleMissingParameters(OAuthUtils.java:184)
at org.apache.amber.oauth2.client.validator.OAuthClientValidator.validateRequiredParameters(OAuthClientValidator.java:90)
at org.apache.amber.oauth2.client.validator.OAuthClientValidator.validateParameters(OAuthClientValidator.java:53)
at org.apache.amber.oauth2.client.validator.OAuthClientValidator.validate(OAuthClientValidator.java:49)
at org.apache.amber.oauth2.client.response.OAuthClientResponse.validate(OAuthClientResponse.java:64)
at org.apache.amber.oauth2.client.response.OAuthClientResponse.init(OAuthClientResponse.java:59)
at org.apache.amber.oauth2.client.response.OAuthAccessTokenResponse.init(OAuthAccessTokenResponse.java:52)
at org.apache.amber.oauth2.client.response.OAuthClientResponseFactory.createCustomResponse(OAuthClientResponseFactory.java:60)
at org.apache.amber.oauth2.client.URLConnectionClient.execute(URLConnectionClient.java:105)

请帮助解决这个问题。提前感谢您的帮助。

【问题讨论】:

    标签: java oauth-2.0 linkedin


    【解决方案1】:

    我发现了问题。

    文档中似乎有问题。 道歉。

    正确的调用是

    request = OAuthClientRequest.tokenLocation("https://www.linkedin.com/uas/oauth2/accessToken")
                        .setGrantType(ResponseType.CODE.toString())
                        .setCode("****")
                        .setRedirectURI("https://www.example.in")
                        .setClientId("*******")
                        .setClientSecret("******").buildBodyMessage();
    

    注意ResponseType.CODE.toString() 而不是GrantType.AUTHORIZATION_CODE

    【讨论】:

    • 谢谢安东尼奥,刚试过这个并得到编译错误 OAuthClientRequest.TokenRequestBuilder 类型中的方法 setGrantType(GrantType) 不适用于参数(字符串)。我需要做任何一周吗?
    • 哎呀抱歉。我将身份验证位置与令牌之一混合在一起。问题是您可能想要使用 OAuthJSONAccessTokenResponse.class 而不是 GitHubTokenResponse.class 用于 Linkedin: OAuthJSONAccessTokenResponse oAuthResponse = oAuthClient.accessToken(request, OAuthJSONAccessTokenResponse.class);我们正在改进我们的 API 和文档。我确实意识到这有点令人困惑。
    • 谢谢安东尼奥,我终于可以打电话了。
    • @Bhabani, Antonio - 请问我们需要设置哪些范围细节? OAuthBearerClientRequest 请求 url 是什么?请回复
    【解决方案2】:

    我建议您阅读一些linkedin 开发人员文档here,并查看java OAuth library,而不是手动执行签名过程。然后你可以问一个具体的问题。

    【讨论】:

      猜你喜欢
      • 2012-12-30
      • 2018-06-24
      • 2012-07-16
      • 2012-08-14
      • 1970-01-01
      • 1970-01-01
      • 2012-03-10
      • 2011-12-22
      相关资源
      最近更新 更多