【问题标题】:OAuth call for a request token yields a 400 error请求令牌的 OAuth 调用产生 400 错误
【发布时间】:2015-05-18 08:21:51
【问题描述】:

我想使用刚刚获得的授权码为我的应用程序获取访问令牌。我正在使用此代码

DefaultHttpClient client = new DefaultHttpClient();
URI uri = new URIBuilder().setScheme("https")
        .setHost("www.linkedin.com")
        .setPath("/uas/oauth2/accessToken")
        .setParameter("grant_type", "authorization_code")
        .setParameter("code", code)
        .setParameter("redirect_uri", "http://localhost:9090/ConnectSocialMedia/callBack.jsp")
        .setParameter("client_id", CONSUMER_KEY_OPTION)
        .setParameter("client_secret", CONSUMER_SECRET_OPTION)
        .build();
HttpPost post = new HttpPost(uri);
post.setHeader("Content-Type", "application/x-www-form-urlencoded");
HttpResponse response2 = client.execute(post);
System.out.println("Final Response------------>"+response2);

但我收到 HTTP/1.1 400 错误请求。我做错了什么?

任何帮助将不胜感激!

【问题讨论】:

    标签: oauth-2.0 linkedin-j


    【解决方案1】:

    您将参数作为 URL 中的查询参数添加到令牌端点,但它们应该在 POST 参数中传递。你应该使用类似的东西:

    DefaultHttpClient client = new DefaultHttpClient();
    URI uri = new URIBuilder().setScheme("https")
        .setHost("www.linkedin.com")
        .setPath("/uas/oauth2/accessToken")
        .build();
    
    List <NameValuePair> nvps = new ArrayList <NameValuePair>();
    nvps.add(new BasicNameValuePair("grant_type", "authorization_code"));
    nvps.add(new BasicNameValuePair("code", code));
    nvps.add(new BasicNameValuePair("redirect_uri", "http://localhost:9090/ConnectSocialMedia/callBack.jsp"));
    nvps.add(new BasicNameValuePair("client_id", CONSUMER_KEY_OPTION));
    nvps.add(new BasicNameValuePair("client_secret", CONSUMER_SECRET_OPTION));
    
    HttpPost post = new HttpPost(uri);
    post.setEntity(new UrlEncodedFormEntity(nvps));
    HttpResponse response2 = client.execute(post);
    System.out.println("Final Response------------>"+response2);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-21
      • 1970-01-01
      • 1970-01-01
      • 2012-11-06
      • 1970-01-01
      • 2016-12-08
      • 2023-01-14
      • 1970-01-01
      相关资源
      最近更新 更多