Java linkedin中Http请求中的授权标头

Java linkedin中Http请求中的授权标头,java,json,jsp,servlets,Java,Json,Jsp,Servlets,您好,在我的servlet代码中,我代表用户请求带有access_令牌的服务器,我可以使用以下代码请求: 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/~

您好,在我的servlet代码中,我代表用户请求带有access_令牌的服务器,我可以使用以下代码请求:

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);

谢谢

您可以使用
apache.http
库来实现这一点。您不需要一些OAuth库或任何东西。OAuth协议非常“容易”处理,您可以使用普通http请求来处理它。下面是apache http库的一个示例

[EDIT]我更改了代码,为您提供了一个完整的示例,即如何使用这些库

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
文件,您可以将其添加为库:


您还可以从

正如您将看到的,这些库为您提供了处理访问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
     ...

大家好,GameDroids,我在导入包处收到错误:导入org.apache.http.impl.client.HttpClientBuilder无法解决我添加了以下jar:commons-httpclient-3.1.jar apache-httpcomponents-httpclient.jar我在:HttpClientBuilder.create().build()处收到编译错误@对不起,我花了这么长时间。我编辑了我的答案并添加了一个完整的例子。此外,我还查找了正确的库,它们在较新的版本中发生了一些更改,我想这就是您的错误的来源。希望它能帮助您了解游戏机器人,它是静态工作的,但当我在servlet中使用它时,它在HttpClientBuilder中抛出了一个错误。我能够通过以下方式进行管理:OAuthRequestRequest2=newOAuthRequest(动词.GET,”);request2.addHeader(“授权”、“承载人”+accesstok);Response response2=request2.send();
httpcomponents-client-4.3.6
   +-examples
   +-lib
      +- commons-codec-1.6.jar
      +- ...
      +- httpclient-4.3.6.jar  <-- this one
      +- ...