Bạn có thể đạt được điều này bằng cách sử dụng jar bên thứ ba scribe.jar. Gọi mục đích xem web để ủy quyền như sau.
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);
Khi ủy quyền, bạn sẽ được chuyển hướng đến hoạt động của mình. Truy xuất mã thông báo truy cập trong hoạt động của bạn như sau.
@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);
}
Sử dụng mã thông báo truy cập bạn có thể cập nhật mạng thành linkedin như sau.
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());
}
Hoạt động phải được khai báo trong tệp kê khai như sau.
<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>
Xin chào Thasneem Tôi đang tìm tích hợp liên kết với Android bằng Scribe. Bạn có thể xin vui lòng gửi cho tôi một ứng dụng mẫu nếu bạn có hoặc hướng dẫn tôi mặc dù một liên kết có liên quan ?? –
Vui lòng xem url sau để biết thêm thông tin. https: // github.com/fernandezpablo85/scribe-java/blob/master/src/test/java/org/scribe/ví dụ/LinkedInExample.java. Bạn có thể tải xuống ứng dụng mẫu từ đây. – Thasneem