[英]Authorization header in Http Request in linkedin
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: 我使用以下方式,但不是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 谢谢
You could use the apache.http
library for that. 您可以使用
apache.http
库。 You don't need some OAuth-library or anything. 您不需要一些OAuth库或任何东西。 The OAuth protocol is so 'easy' to handle, that you can do it with normal http requests.
OAuth协议非常容易处理,您可以使用普通的http请求来执行此操作。 Here an example with the apache-http library.
这是一个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 jar
files which you can add as a library: 在这里,您可以找到可以添加为库的
jar
文件:
Apache HTTP-Core v 4.3.3 Apache HTTP-Core v 4.3.3
Apache HTTP-Client v 4.3.6 Apache HTTP-Client v 4.3.6
You can also download that jars from the Apache page 您也可以从Apache页面下载该jar文件
As you will see, the libraries provide you with everything to handle all Requests you may need to access the API ( GET POST DELETE..
). 正如您将看到的,这些库为您提供了处理访问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.
我知道它看起来很复杂,但有了这个,您就可以完全控制OAuth请求,而不需要依赖任何库。
[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. 当您从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
+- ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.