android - 来自 linkedin 的 Android 应用程序访问 token

标签 android oauth linkedin

首先。是否可以在 facebook、twitter 等 android 应用程序中添加 linkedin?我已经阅读了很多博客,但无法在我的应用程序中实现 linkedin。我已经到达用户输入用户名和密码的应用程序的用户授权过程。但是当他输入一个 5 位数的数字时,屏幕上出现了,屏幕被告知进入了应用程序主屏幕。然后填写它并按回车键。

但问题是我如何从浏览器返回到我的应用程序以及用户应该将这些数字数据放在哪里。以及何时以及如何获得访问 token 以使用用户配置文件的数据。

互联网上没有什么好的东西可以用于 linkedin 和 android。我有一个图书馆 http://code.google.com/p/linkedin-j/但是如何克服这种情况呢?不知道。谁能建议我一些解决方案。谢谢。

最佳答案

您可以使用第三方 jar scribe.jar 实现此目的。 如下调用 webview intent 进行授权。

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

授权后,您将被重定向到您的 Activity 。按如下方式在您的 Activity 中检索访问 token 。

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

使用访问 token ,您可以按如下方式对 linkedin 进行网络更新。

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

}

Activity 应在 list 文件中声明如下。

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

关于android - 来自 linkedin 的 Android 应用程序访问 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6453788/

相关文章:

php - 如何使用 cURL 和 PHP 抓取 LinkedIn 公司页面? No CSRF token found in headers 错误

facebook - 最好的用户界面/设计模式来找到我的用户的 friend 也是我的用户

Android:对所有版本的android应用相同的外观?

Android Firebase 存储示例

Android 应用程序在使用 Proguard 和 Dagger 的 Release Build 上崩溃

Facebook Canvas iFrame App - 使用新的 OAuth 协议(protocol)授权用户

oauth - 缩短社交登录流程

android - 使用联合登录 (OpenID) 从 Android 应用程序对 App Engine 进行身份验证

rest - Linkedin REST API 检索用户的状态更新

android - 当我的应用程序启动时,如何在没有预览的情况下从相机拍照?