在linkedin中的Http Request中的授权标头 [英] Authorization header in Http Request in linkedin

查看:153
本文介绍了在linkedin中的Http Request中的授权标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我的servlet代码我代表用户使用access_token请求服务器我可以使用以下代码请求:

Hi in my servlet code I am requesting the server with access_token on behalf of user i am able to request with below code:

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

但我如何使用下面的授权标题请求:

But How i can request with Authorization Header like below:

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

我使用的方式如下,但不是wrking:

I am using in the below way but not wrking:

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 库的示例。

You could use the apache.http library for that. You don't need some OAuth-library or anything. The OAuth protocol is so 'easy' to handle, that you can do it with normal http requests. Here an example with the apache-http library.

我更改了代码给你一个完整示例,如何使用这些库。

I changed the code to give you a full example, how to use those libraries.

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 文件作为一个图书馆:

And here you can find the jar files which you can add as a library:

Apache HTTP-Core v 4.3.3

Apache HTTP-Client v 4.3.6

您也可以从 Apache页面下载该罐子

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

As you will see, the libraries provide you with everything to handle all Requests you may need to access the API (GET POST DELETE..). You can change the headers and handle what ever content you get as a response. I know it looks complicated, but with this you will have full control over you OAuth requests and don't need to rely on any library.

[又一个编辑]

当你从Apache页面下载zip文件时,你需要解压缩它们您需要的 jar 文件位于 lib 文件夹中。

[Yet another EDIT]
When you download the zip files from the Apache page you need to unpack them and the jar file you need is within the lib folder.

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
      +- ...

这篇关于在linkedin中的Http Request中的授权标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆