【问题标题】:Authorization header in Http Request in linkedin链接中的 Http 请求中的授权标头
【发布时间】:2015-01-27 19:40:41
【问题描述】:

嗨,在我的 servlet 代码中,我代表用户请求带有 access_token 的服务器,我可以使用以下代码请求:

OAuthRequest request2 = new OAuthRequest(Verb.GET,"https://api.linkedin.com/v1/people/~:(first-name,last-name,email-address)?oauth2_access_token="+accesstok);

但我如何使用如下授权标头请求:

GET /v1/people/~ HTTP/1.1
Host: api.linkedin.com
Connection: Keep-Alive
Authorization: Bearer AQXdSP_W41_UPs5ioT_t8HESyODB4FqbkJ8LrV_5mff4gPODzOYR

我正在使用以下方式但不工作:

private static final String PROTECTED_RESOURCE_URL = "/v1/people/~:(first-name,last-   name,email-address) HTTP/1.1 Host: api.linkedin.com Connection: Keep-Alive Authorization: Bearer ";
Object AccessToken=  o.get("access_token"); 

String accesstok=AccessToken.toString();

OAuthRequest request2 = new OAuthRequest(Verb.GET,PROTECTED_RESOURCE_URL+accesstok);

谢谢

【问题讨论】:

    标签: java json jsp servlets


    【解决方案1】:

    您可以为此使用apache.http 库。您不需要一些 OAuth 库或任何东西。 OAuth 协议非常“容易”处理,您可以使用普通的 http 请求来完成。这是 apache-http 库的示例。

    [编辑]我更改了代码,为您提供了一个完整的示例,说明如何使用这些库。

    import java.io.IOException;
    import org.apache.http.HttpResponse;
    import org.apache.http.ParseException;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.HttpClientBuilder;
    import org.apache.http.util.EntityUtils;
    
    public class OAuthConnect {
    
        public static void main(String[] args) {
            try {
                HttpClient httpclient = HttpClientBuilder.create().build();  // the http-client, that will send the request
                HttpGet httpGet = new HttpGet("");   // the http GET request
                httpGet.addHeader("Authorization", "Bearer AQXdSP_W41_UPs5ioT_t8HESyODB4FqbkJ8LrV_5mff4gPODzOYR"); // add the authorization header to the request
                HttpResponse response = httpclient.execute(httpGet); // the client executes the request and gets a response
                int responseCode = response.getStatusLine().getStatusCode();  // check the response code
                switch (responseCode) {
                    case 200: { 
                        // everything is fine, handle the response
                        String stringResponse = EntityUtils.toString(response.getEntity());  // now you have the response as String, which you can convert to a JSONObject or do other stuff
                        break;
                    }
                    case 500: {
                        // server problems ?
                        break;
                    }
                    case 403: {
                        // you have no authorization to access that resource
                        break;
                    }
                }
            } catch (IOException | ParseException ex) {
                // handle exception
            }
        }
    }
    

    在这里您可以找到可以添加为库的jar 文件:

    Apache HTTP-Core v 4.3.3
    Apache HTTP-Client v 4.3.6

    你也可以从Apache page下载那个jar

    正如您将看到的,这些库为您提供了处理访问 API (GET POST DELETE..) 可能需要的所有请求的一切。您可以更改标题并处理您收到的任何内容作为响应。我知道它看起来很复杂,但是这样你就可以完全控制你的 OAuth 请求并且不需要依赖任何库。

    [又一次编辑]
    当您从 Apache 页面下载 zip 文件时,您需要解压缩它们,并且您需要的 jar 文件位于 lib 文件夹中。

    httpcomponents-core-4.3.3
       +-examples
       +-lib
          +-commons-cli-1.2.jar
          +-httpcore-4.3.3.jar   <-- this one you need
          +-httpcore-ab-4.3.3.jar
         ...
    

    httpClient一样

    httpcomponents-client-4.3.6
       +-examples
       +-lib
          +- commons-codec-1.6.jar
          +- ...
          +- httpclient-4.3.6.jar  <-- this one
          +- ...
    

    【讨论】:

    • 嗨 GameDroids,我在导入包时遇到错误:无法解析导入 org.apache.http.impl.client.HttpClientBuilder 我添加了以下 jar:commons-httpclient-3.1.jar apache- httpcomponents-httpclient.jar
    • 我在以下位置遇到编译错误:HttpClientBuilder.create().build();
    • @Raj:对不起,我花了这么长时间。我编辑了我的答案并添加了一个完整的例子。我还查找了正确的库——它们在较新的版本中发生了一些变化,我想这就是你的错误的来源。希望对你有帮助
    • 谢谢 GameDroids,它可以静态工作,但是当我在 servlet 中使用时,它在 HttpClientBuilder 处引发错误。我能够通过以下方式进行管理: OAuthRequest request2 = new OAuthRequest(Verb.GET,"api.linkedin.com/v1/people/…); request2.addHeader("Authorization", "Bearer "+accesstok); Response response2 = request2.send();
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-19
    • 2016-06-30
    • 2011-05-04
    • 2017-08-07
    • 2014-05-14
    • 2017-12-25
    相关资源
    最近更新 更多