从linkedin访问Android应用程序的令牌 [英] access token from linkedin for android application
问题描述
首先.是否可以使用Android应用程序(例如facebook,twitter)添加linkedin?我已经阅读了许多博客,但是无法在我的应用程序中实现linkedin.我已经进入应用程序的用户授权过程,在该过程中,用户输入了他的用户名和密码.但是当他输入5位数字时,屏幕上会出现,并且屏幕提示已到达应用程序主屏幕.然后填充它,然后按Enter.
First of all. Is it possible to add linkedin with android application like facebook,twitter ? I have read many of blogs but can't able to implement linkedin in my application. I have reached to the user authorization process for application where user enter his user name and password. but when he enters a 5 digits number come on screen and screen told got to the application home screen. Then fill it and press enter.
但是问题是,我该如何从浏览器移回我的应用程序,以及用户应在何处放置此数字数据.而当&我如何获取访问令牌以使用用户个人资料的数据.
But question is there how can i move back from browser to my app and where user should put this numerical data . And when & how can i get access token to use data of the user profile.
在互联网上没有什么好事可用于android的linkedin.我有一个图书馆 http://code.google.com/p/linkedin-j/但是如何从情况中克服?不知道.谁能建议我一些解决方案.谢谢.
There are no good matters on internet to use for linkedin with android. I got one of library http://code.google.com/p/linkedin-j/ but how overcome from situation ? No Idea. Can anyone suggest me some solution. Thanks.
推荐答案
您可以使用第三方jar scribe.jar来实现. 调用Webview意图进行授权,如下所示.
You can achieve this using a third party jar scribe.jar. Call webview intent for authorization as follows.
OAuthService service = new ServiceBuilder()
.provider(LinkedInApi.class).apiKey(Constants.CONSUMER_KEY)
.apiSecret(Constants.CONSUMER_SECRET)
.callback(Constants.OAUTH_CALLBACK_URL).build();
Token liToken = oAuthService
.getRequestToken();
String url = oAuthService
.getAuthorizationUrl(PrepareRequestLinkedinTokenActivity.liToken);
Log.i(TAG, "Popping a browser with the authorize URL : " + url);
// Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(liToken
// .getAuthorizationUrl()));
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
context.startActivity(intent);
授权后,您将被重定向到您的活动.检索活动中的访问令牌,如下所示.
On authorizing, you will be redirected to your activity. Retrieve the access token in your activity as follows.
@Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(this);
final Uri uri = intent.getData();
if (uri != null
&& uri.getScheme().equals(Constants.OAUTH_CALLBACK_SCHEME)) {
Log.i(TAG, "Callback received : " + uri);
Log.i(TAG, "Retrieving Access Token");
new RetrieveAccessTokenTask(this, prefs).execute(uri);
finish();
}
}
public class RetrieveAccessTokenTask extends AsyncTask<Uri, Void, Void> {
private SharedPreferences prefs;
public RetrieveAccessTokenTask(Context context, SharedPreferences prefs) {
this.prefs = prefs;
}
/**
* Retrieve the oauth_verifier, and store the oauth and
* oauth_token_secret for future API calls.
*/
@Override
protected Void doInBackground(Uri... params) {
final Uri uri = params[0];
final Verifier verifier = new Verifier(
uri.getQueryParameter("oauth_verifier"));
try {
accessToken = service.getAccessToken(liToken, verifier);
final Editor edit = prefs.edit();
edit.putString(Constants.LINKEDIN_TOKEN, accessToken.getToken());
edit.putString(Constants.LINKEDIN_TOKEN_SECRET,
accessToken.getSecret());
edit.commit();
Log.i(TAG, "OAuth - Access Token Retrieved");
} catch (Exception e) {
Log.e(TAG, "OAuth - Access Token Retrieval Error", e);
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
executeAfterAccessTokenRetrieval(accessToken);
}
使用访问令牌,您可以按照以下步骤对linkedin进行网络更新.
Using the access token you can make network updates to linkedin as follows.
private void postToLinkedin(String comment) {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(LinkedinDialogActivity.this);
String token = prefs.getString(Constants.LINKEDIN_TOKEN, "");
String secret = prefs.getString(Constants.LINKEDIN_TOKEN_SECRET, "");
Token accessToken = new Token(token, secret);
OAuthService service = new ServiceBuilder().provider(LinkedInApi.class)
.apiKey(Constants.CONSUMER_KEY)
.apiSecret(Constants.CONSUMER_SECRET)
.callback(Constants.OAUTH_CALLBACK_URL).build();
String url = "http://api.linkedin.com/v1/people/~/shares";
OAuthRequest request = new OAuthRequest(Verb.POST, url);
String payLoad = "<?xml version='1.0' encoding='UTF-8'?><share><comment>Check out the Sep 13 Second share!</comment><content><title>My new share with linked-in</title><description>Leverage the Share API to maximize engagement on user-generated content on LinkedIn</description><submitted-url>https://developer.linkedin.com/documents/share-api</submitted-url><submitted-image-url>http://m3.licdn.com/media/p/3/000/124/1a6/089a29a.png</submitted-image-url></content><visibility><code>anyone</code></visibility></share>";
request.addHeader("Content-Length", Integer.toString(payLoad.length()));
request.addHeader("Content-Type", "text/xml");
request.addPayload(payLoad);
service.signRequest(accessToken, request);
Response response = request.send();
System.out.println("response >>>> " + response.getBody());
}
活动应在清单文件中声明如下.
The activity should be declared in manifest file as follows.
<activity android:name=".PrepareRequestLinkedinTokenActivity"
android:launchMode="singleTask" android:theme="@android:style/Theme.Translucent.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:host="callback" android:scheme="x-oauthflow-linkedin" />
</intent-filter>
</activity>
这篇关于从linkedin访问Android应用程序的令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!