LinkedIn 集成 - 建立 requestToken
我正在开发(目前正在尝试)将与 LinkedIn 集成的 portlet。
按照有关它的文档: http://developer.linkedin.com/docs/DOC-1008 --> ; 授权 LinkedIn 会员的第一步是请求 requestToken。该请求通过 HTTP POST 完成。 对于 requestToken 步骤,您的签名字符串中应包含以下组件:
* HTTP Method (POST)
* Request URI (https://api.linkedin.com/uas/oauth/requestToken)
* oauth_callback
* oauth_consumer_key
* oauth_nonce
* oauth_signature_method
* oauth_timestamp
* oauth_version
我已经有 API(它是 oauth_consumer_key)密钥,并且我需要生成特定的 URL 字符串。 有这个 URL 和 HTTP 连接的下一个 java 代码:
private void processAuthentication() {
Calendar cal = Calendar.getInstance();
Long ms = cal.getTimeInMillis();
Long timestamp = ms / 1000;
Random r = new Random();
Long nonce = r.nextLong();
String prefixUrl = "https://api.linkedin.com/uas/oauth/requestToken";
String oauthCallback = "oauth_callback=http://localhost/";
String oauthConsumerKey =
"&oauth_consumer_key=my_consumer_key";
String oauthNonce = "&oauth_nonce=" + nonce.toString();
String oauthSignatureMethod = "&oauth_signature_method=HMAC-SHA1";
String oauthTimestamp = "&oauth_timestamp=" + timestamp.toString();
String oauthVersion = "&oauth_version=1.0";
String mainUrl =
oauthCallback + oauthConsumerKey + oauthNonce + oauthSignatureMethod
+ oauthTimestamp + oauthVersion;
try {
prefixUrl =
URLEncoder.encode(prefixUrl, "UTF-8") + "&"
+ URLEncoder.encode(mainUrl, "UTF-8");
URL url = new URL(prefixUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
String msg = connection.getResponseMessage();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
对于那些遇到过这个问题的人来说,下一个问题是: 应该如何真正查看连接的 URL 字符串以及如何接收响应?
对于 URL,它对您生成的 URL 示例感兴趣。 对于感兴趣的响应,获取它的方法。 据我了解,建立 HTTP 连接后,响应是:
connection.getResponseMessage();
I'm developing (trying for now) portlet that will be integrated with LinkedIn.
Following the documentation about it:
http://developer.linkedin.com/docs/DOC-1008 -->
The first step to authorizing a LinkedIn member is requesting a requestToken. This request is done with an HTTP POST.
For the requestToken step, the following components should be present in your string to sign:
* HTTP Method (POST)
* Request URI (https://api.linkedin.com/uas/oauth/requestToken)
* oauth_callback
* oauth_consumer_key
* oauth_nonce
* oauth_signature_method
* oauth_timestamp
* oauth_version
I have already API(it's oauth_consumer_key) key and i need to generate specific URL string.
Have next java code for this URL and HTTP connection:
private void processAuthentication() {
Calendar cal = Calendar.getInstance();
Long ms = cal.getTimeInMillis();
Long timestamp = ms / 1000;
Random r = new Random();
Long nonce = r.nextLong();
String prefixUrl = "https://api.linkedin.com/uas/oauth/requestToken";
String oauthCallback = "oauth_callback=http://localhost/";
String oauthConsumerKey =
"&oauth_consumer_key=my_consumer_key";
String oauthNonce = "&oauth_nonce=" + nonce.toString();
String oauthSignatureMethod = "&oauth_signature_method=HMAC-SHA1";
String oauthTimestamp = "&oauth_timestamp=" + timestamp.toString();
String oauthVersion = "&oauth_version=1.0";
String mainUrl =
oauthCallback + oauthConsumerKey + oauthNonce + oauthSignatureMethod
+ oauthTimestamp + oauthVersion;
try {
prefixUrl =
URLEncoder.encode(prefixUrl, "UTF-8") + "&"
+ URLEncoder.encode(mainUrl, "UTF-8");
URL url = new URL(prefixUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
String msg = connection.getResponseMessage();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
The question is next,for those, who had faced this problem:
How should really look URL string for connection and how response is received?
For URL, it's interested the example of URL, you generated.
And for response interested, method to get it.
As i understand, after HTTP connection been established,that response is:
connection.getResponseMessage();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
@sergionni 我从 linkedin-developer 找到了你问题的答案
如你所知
授权 Linked-In 成员的第一步是请求 requestToken。该请求通过 HTTP POST 完成。
如果您使用回调,您的基本字符串最终应该看起来像这样:
然后,您使用 Consumer_secret 签署此基本字符串,计算签名。在这种情况下,如果您的密钥是 1234567890,则签名将为 TLQXuUzM7omwDbtXimn6bLDvfF8=。
现在,您可以使用生成的签名以及 oauth_nonce、oauth_callback、oauth_signature_method、oauth_timestamp、oauth_consumer_key 和 oauth_version 并创建 HTTP 授权标头。对于此请求,该 HTTP 标头将如下所示:
请注意,HTTP 标头是单个标头——而不是每个组件的 HTTP 标头。您可以选择提供领域=“http://api.linkedin.com”。
作为对 requestToken 请求的响应,您的 requestToken 将位于“oauth_token”响应字段中,这是我们使用“oauth_callback_confirmed”字段、oauth_token_secret 和 oauth_expires_in 以及一些其他值确认您的回调的验证。
(这里是您的答案)回复如下:
@sergionni I found answer to your Question from linkedin-developer
As you know
The first step to authorizing a Linked-In member is requesting a requestToken. This request is done with an HTTP POST.
Your base string should end up looking something like this if you're using a callback:
You then sign this base string with your consumer_secret, computing a signature. In this case, if your secret was 1234567890, the signature would be TLQXuUzM7omwDbtXimn6bLDvfF8=.
Now you take the signature you generated, along with oauth_nonce, oauth_callback, oauth_signature_method, oauth_timestamp, oauth_consumer_key, and oauth_version and create an HTTP Authorization header. For this request, that HTTP header would look like:
Please note, that the HTTP header is a single header -- not an HTTP header for each component. You can optionally supply a realm="http://api.linkedin.com".
As a response to your request for a requestToken, your requestToken will be in the "oauth_token" response field, a validation that we acknowledged your callback with the "oauth_callback_confirmed" field, an oauth_token_secret, and a oauth_expires_in, and a few other values.
(here us Your answaer) response would look like:
您可以尝试使用 OAuth 库来处理连接:http://code.google.com/p /oauth/
You might try out the OAuth libraries to handle the connection: http://code.google.com/p/oauth/
我为 Play Framework 创建了一个插件,可以轻松地与 LinkedIn 的 OAuth 集成:geeks.aretotally.in/projects/play-framework-linkedin-module。希望它能有所帮助。你应该看看 Play,非常非常酷的 Java 框架。
I created a plugin for Play Framework to easily integrated with LinkedIn's OAuth: geeks.aretotally.in/projects/play-framework-linkedin-module. Hopefully it can help. You should def check out Play, very very cool Java framework.
门户组件主体:
portlet body: