java Linkedin 中 Http 请求中的授权标头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27200634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:23:17  来源:igfitidea点击:

Authorization header in Http Request in linkedin

javajsonjspservlets

提问by Raj

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:

嗨,在我的 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);

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

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

Thank You

谢谢

回答by GameDroids

You could use the apache.httplibrary 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-httplibrary.

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

[EDIT]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
        }
    }
}

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

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

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

Apache HTTP 核心 v 4.3.3
Apache HTTP 客户端 v 4.3.6

You can also download that jars from the Apache page

您还可以从Apache 页面下载该 jars

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.

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

[Yet another EDIT]
When you download the zip files from the Apache page you need to unpack them and the jarfile you need is within the libfolder.

[又一次编辑]
当您从 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
     ...

and the same with the httpClient

httpClient

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