从 Android 应用程序发布 LinkedIn 消息

Posted

技术标签:

【中文标题】从 Android 应用程序发布 LinkedIn 消息【英文标题】:Posting LinkedIn message from Android application 【发布时间】:2011-08-13 20:08:39 【问题描述】:

我想将我的 android 应用程序与 LinkedIn 集成并发布消息。谁能提供一个例子来说明如何做到这一点?

【问题讨论】:

【参考方案1】:

你甚至尝试过谷歌吗? 来自http://developer.linkedin.com/docs/DOC-1255 我们得到了 http://code.google.com/p/linkedin-j/

编辑: 这是我的示例项目http://esilo.pl/LITest.zip

EDIT2:最小样本,令牌存储在 SharedPreferences 中(因此您无需每次都进行授权(我将更新 LITest.zip))

EDIT3:添加了 AsyncTask 代码...以避免 NetworkOnMainThreadException :)

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="pl.selvin.android.LinkedInTest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="4" />

    <uses-permission android:name="android.permission.INTERNET" />

    <application android:label="LinkedInTest" >
        <activity
            android:name=".LITestActivity"
            android:label="LinkedIn Test"
            android:launchMode="singleInstance" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <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="litestcalback"
                    android:scheme="x-oauthflow-linkedin" />
            </intent-filter>
        </activity>
    </application>

</manifest>

LITestActivity.java:

package pl.selvin.android.LinkedInTest;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import com.google.code.linkedinapi.client.LinkedInApiClient;
import com.google.code.linkedinapi.client.LinkedInApiClientException;
import com.google.code.linkedinapi.client.LinkedInApiClientFactory;
import com.google.code.linkedinapi.client.oauth.LinkedInAccessToken;
import com.google.code.linkedinapi.client.oauth.LinkedInOAuthService;
import com.google.code.linkedinapi.client.oauth.LinkedInOAuthServiceFactory;
import com.google.code.linkedinapi.client.oauth.LinkedInRequestToken;
import com.google.code.linkedinapi.schema.Person;

public class LITestActivity extends Activity 

    // /change keysssssssssssssssssssssssssssss!!!!!!!!!!

    static final String CONSUMER_KEY = "keykeykeykey";
    static final String CONSUMER_SECRET = "secretsecret";

    static final String APP_NAME = "LITest";
    static final String OAUTH_CALLBACK_SCHEME = "x-oauthflow-linkedin";
    static final String OAUTH_CALLBACK_HOST = "litestcalback";
    static final String OAUTH_CALLBACK_URL = String.format("%s://%s",
            OAUTH_CALLBACK_SCHEME, OAUTH_CALLBACK_HOST);
    static final String OAUTH_QUERY_TOKEN = "oauth_token";
    static final String OAUTH_QUERY_VERIFIER = "oauth_verifier";
    static final String OAUTH_QUERY_PROBLEM = "oauth_problem";

    final LinkedInOAuthService oAuthService = LinkedInOAuthServiceFactory
            .getInstance().createLinkedInOAuthService(CONSUMER_KEY,
                    CONSUMER_SECRET);
    final LinkedInApiClientFactory factory = LinkedInApiClientFactory
            .newInstance(CONSUMER_KEY, CONSUMER_SECRET);

    static final String OAUTH_PREF = "LIKEDIN_OAUTH";
    static final String PREF_TOKEN = "token";
    static final String PREF_TOKENSECRET = "tokenSecret";
    static final String PREF_REQTOKENSECRET = "requestTokenSecret";

    TextView tv = null;

    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        tv = new TextView(this);
        setContentView(tv);
        final SharedPreferences pref = getSharedPreferences(OAUTH_PREF,
                MODE_PRIVATE);
        final String token = pref.getString(PREF_TOKEN, null);
        final String tokenSecret = pref.getString(PREF_TOKENSECRET, null);
        if (token == null || tokenSecret == null) 
            startAutheniticate();
         else 
            showCurrentUser(new LinkedInAccessToken(token, tokenSecret));
        

    

    void startAutheniticate() 
        new AsyncTask<Void, Void, LinkedInRequestToken>() 

            @Override
            protected LinkedInRequestToken doInBackground(Void... params) 
                return oAuthService.getOAuthRequestToken(OAUTH_CALLBACK_URL);
            

            @Override
            protected void onPostExecute(LinkedInRequestToken liToken) 
                final String uri = liToken.getAuthorizationUrl();
                getSharedPreferences(OAUTH_PREF, MODE_PRIVATE)
                        .edit()
                        .putString(PREF_REQTOKENSECRET,
                                liToken.getTokenSecret()).commit();
                Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
                startActivity(i);
            
        .execute();
    

    void finishAuthenticate(final Uri uri) 
        if (uri != null && uri.getScheme().equals(OAUTH_CALLBACK_SCHEME)) 
            final String problem = uri.getQueryParameter(OAUTH_QUERY_PROBLEM);
            if (problem == null) 

                new AsyncTask<Void, Void, LinkedInAccessToken>() 

                    @Override
                    protected LinkedInAccessToken doInBackground(Void... params) 
                        final SharedPreferences pref = getSharedPreferences(
                                OAUTH_PREF, MODE_PRIVATE);
                        final LinkedInAccessToken accessToken = oAuthService
                                .getOAuthAccessToken(
                                        new LinkedInRequestToken(
                                                uri.getQueryParameter(OAUTH_QUERY_TOKEN),
                                                pref.getString(
                                                        PREF_REQTOKENSECRET,
                                                        null)),
                                        uri.getQueryParameter(OAUTH_QUERY_VERIFIER));
                        pref.edit()
                                .putString(PREF_TOKEN, accessToken.getToken())
                                .putString(PREF_TOKENSECRET,
                                        accessToken.getTokenSecret())
                                .remove(PREF_REQTOKENSECRET).commit();
                        return accessToken;
                    

                    @Override
                    protected void onPostExecute(LinkedInAccessToken accessToken) 
                        showCurrentUser(accessToken);
                    
                .execute();

             else 
                Toast.makeText(this,
                        "Appliaction down due OAuth problem: " + problem,
                        Toast.LENGTH_LONG).show();
                finish();
            

        
    

    void clearTokens() 
        getSharedPreferences(OAUTH_PREF, MODE_PRIVATE).edit()
                .remove(PREF_TOKEN).remove(PREF_TOKENSECRET)
                .remove(PREF_REQTOKENSECRET).commit();
    

    void showCurrentUser(final LinkedInAccessToken accessToken) 
        final LinkedInApiClient client = factory
                .createLinkedInApiClient(accessToken);

        new AsyncTask<Void, Void, Object>() 

            @Override
            protected Object doInBackground(Void... params) 
                try 

                    final Person p = client.getProfileForCurrentUser();
                    // /////////////////////////////////////////////////////////
                    // here you can do client API calls ...
                    // client.postComment(arg0, arg1);
                    // client.updateCurrentStatus(arg0);
                    // or any other API call
                    // (this sample only check for current user
                    // and pass it to onPostExecute)
                    // /////////////////////////////////////////////////////////
                    return p;
                 catch (LinkedInApiClientException ex) 
                    return ex;
                
            

            @Override
            protected void onPostExecute(Object result) 
                if (result instanceof Exception) 
                    //result is an Exception :) 
                    final Exception ex = (Exception) result;
                    clearTokens();
                    Toast.makeText(
                            LITestActivity.this,
                            "Appliaction down due LinkedInApiClientException: "
                                    + ex.getMessage()
                                    + " Authokens cleared - try run application again.",
                            Toast.LENGTH_LONG).show();
                    finish();
                 else if (result instanceof Person) 
                    final Person p = (Person) result;
                    tv.setText(p.getLastName() + ", " + p.getFirstName());
                
            
        .execute();

    

    @Override
    protected void onNewIntent(Intent intent) 
        finishAuthenticate(intent.getData());
    

【讨论】:

是的,我试试看.. 并从 google svn 结帐。但是在这个链接中code.google.com/p/linkedin-j/wiki/GettingStarted accessTokenValue 和 tokenSecretValue 是什么意思? 我也使用了它,但是在获取 requestToken 时出现错误,我用代码developer.linkedin.com/thread/3034 plz help 发布我的问题 样本基于code.google.com/p/linkedin-j/wiki/AndroidConfiguration => esilo.pl/LITest.zip ... 15 分钟完成 您好 Selvin,请帮帮我...我在这里发布了问题...***.com/questions/8562584/… 嗨 Selvin 在这个例子中我得到了名字,姓氏,但是如何获取登录的特定用户的 ID ...所以告诉你这样做..【参考方案2】:

非常有帮助的答案@Selvin!

此代码应该在 Honeycomb (API 11) 之前的目标上正常工作,但它会在(发布)API 11 上出现 NetworkOnMainThreadException 失败。在 IAPI 11 之前,最好避免在 UI 线程上运行(可能很长的)网络活动,因为它可能会锁定它。

我更新了 Selvin 的代码,使其与 API 10 兼容。

package com.package.my;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.google.code.linkedinapi.client.LinkedInApiClient;
import com.google.code.linkedinapi.client.LinkedInApiClientException;
import com.google.code.linkedinapi.client.LinkedInApiClientFactory;
import com.google.code.linkedinapi.client.oauth.LinkedInAccessToken;
import com.google.code.linkedinapi.client.oauth.LinkedInOAuthService;
import com.google.code.linkedinapi.client.oauth.LinkedInOAuthServiceFactory;
import com.google.code.linkedinapi.client.oauth.LinkedInRequestToken;
import com.google.code.linkedinapi.schema.Person;

public class your_class_name extends Activity 
    public static final String CONSUMER_KEY             = "your_app_key";
    public static final String CONSUMER_SECRET          = "your_app_secret";    
    public static final String APP_NAME                 = "your app name";
    public static final String OAUTH_CALLBACK_SCHEME    = "x-oauthflow-linkedin";
    public static final String OAUTH_CALLBACK_HOST      = "litestcalback";
    public static final String OAUTH_CALLBACK_URL       = OAUTH_CALLBACK_SCHEME + "://" + OAUTH_CALLBACK_HOST;
    static final String OAUTH_QUERY_TOKEN               = "oauth_token";
    static final String OAUTH_QUERY_VERIFIER            = "oauth_verifier";
    static final String OAUTH_QUERY_PROBLEM             = "oauth_problem";
    static final String OAUTH_PREF                      = "AppPreferences";
    static final String PREF_TOKEN                      = "linkedin_token";
    static final String PREF_TOKENSECRET                = "linkedin_token_secret";
    static final String PREF_REQTOKENSECRET             = "linkedin_request_token_secret";

    final LinkedInOAuthService oAuthService             = LinkedInOAuthServiceFactory.getInstance().createLinkedInOAuthService(CONSUMER_KEY, CONSUMER_SECRET);
    final LinkedInApiClientFactory factory              = LinkedInApiClientFactory.newInstance(CONSUMER_KEY, CONSUMER_SECRET);
    LinkedInRequestToken liToken;
    LinkedInApiClient client;

    TextView tv = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        tv = new TextView(this);
        setContentView(tv);
        final SharedPreferences pref    = getSharedPreferences(OAUTH_PREF, MODE_PRIVATE);
        final String token              = pref.getString(PREF_TOKEN, null);
        final String tokenSecret        = pref.getString(PREF_TOKENSECRET, null);
        if (token == null || tokenSecret == null) 
            startAutheniticate();
         else 
            LinkedInAccessToken accessToken = new LinkedInAccessToken(token, tokenSecret);
            showCurrentUser(accessToken);
        
    //end method

    void startAutheniticate() 
        new Thread()//added because this will make code work on post API 10 
            @Override
            public void run()
                final LinkedInRequestToken liToken  = oAuthService.getOAuthRequestToken(OAUTH_CALLBACK_URL); 
                final String uri                    = liToken.getAuthorizationUrl();
                final SharedPreferences pref        = getSharedPreferences(OAUTH_PREF, MODE_PRIVATE);
                SharedPreferences.Editor editor     = pref.edit(); 
                editor.putString(PREF_REQTOKENSECRET, liToken.getTokenSecret());
                editor.commit();
                Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
                startActivity(i);
             
        .start();
    //end method

    void finishAuthenticate(final Uri uri) 
        new Thread()
            @Override
            public void run()
                Looper.prepare();
                if (uri != null && uri.getScheme().equals(OAUTH_CALLBACK_SCHEME)) 
                    final String problem = uri.getQueryParameter(OAUTH_QUERY_PROBLEM);
                    if (problem == null) 
                        final SharedPreferences pref                = getSharedPreferences(OAUTH_PREF, MODE_PRIVATE);
                        final String request_token_secret           = pref.getString(PREF_REQTOKENSECRET, null);
                        final String query_token                    = uri.getQueryParameter(OAUTH_QUERY_TOKEN);
                        final LinkedInRequestToken request_token    = new LinkedInRequestToken(query_token, request_token_secret);
                        final LinkedInAccessToken accessToken       = oAuthService.getOAuthAccessToken(request_token, uri.getQueryParameter(OAUTH_QUERY_VERIFIER));
                        SharedPreferences.Editor editor = pref.edit(); 
                        editor.putString(PREF_TOKEN, accessToken.getToken());
                        editor.putString(PREF_TOKENSECRET, accessToken.getTokenSecret());
                        editor.remove(PREF_REQTOKENSECRET);
                        editor.commit();
                        showCurrentUser(accessToken);
                     else 
                        Toast.makeText(getApplicationContext(), "Application down due OAuth problem: " + problem, Toast.LENGTH_LONG).show();
                        finish();
                    
                
                Looper.loop();
            
        .start();
    //end method

    void clearTokens() 
        getSharedPreferences(OAUTH_PREF, MODE_PRIVATE).edit().remove(PREF_TOKEN).remove(PREF_TOKENSECRET).remove(PREF_REQTOKENSECRET).commit();
    //end method

    void showCurrentUser(final LinkedInAccessToken accessToken) 
        new Thread()
            @Override
            public void run()
                Looper.prepare();
                final LinkedInApiClient client = factory.createLinkedInApiClient(accessToken);
                try 
                    final Person p = client.getProfileForCurrentUser();
                    // /////////////////////////////////////////////////////////
                    // here you can do client API calls ...
                    // client.postComment(arg0, arg1);
                    // client.updateCurrentStatus(arg0);
                    // or any other API call (this sample only check for current user
                    // and shows it in TextView)
                    // /////////////////////////////////////////////////////////             
                    runOnUiThread(new Runnable() //updating UI thread from different thread not a good idea...
                        public void run() 
                            tv.setText(p.getLastName() + ", " + p.getFirstName());
                        
                    );
                    //or use Toast
                    //Toast.makeText(getApplicationContext(), "Lastname:: "+p.getLastName() + ", First name: " + p.getFirstName(), 1).show();
                 catch (LinkedInApiClientException ex) 
                    clearTokens();
                    Toast.makeText(getApplicationContext(),
                        "Application down due LinkedInApiClientException: "+ ex.getMessage() + " Authokens cleared - try run application again.",
                        Toast.LENGTH_LONG).show();
                    finish();
                
                Looper.loop();
            
        .start();
    //end method

    @Override
    protected void onNewIntent(Intent intent) 
        finishAuthenticate(intent.getData());
    //end method
//end class

希望有人觉得这很有用!归功于 Selvin。

【讨论】:

你绝对有用.. 因为 NetworkOnMainThreadException 将在 api 级别 10 之后发生.. 但它在 api 级别 11 以下运行良好。 @dmmh 当我制作这个样本时,没有使用 API 11 的设备 :) 很好的解决方案,但你测试了吗?我不确定tv.setText 是否可以在非 UI 线程上完成... 好的,好吧,我在我的答案中修正了我愚蠢的编码错误......现在一切都经过测试并且正在运行!再次感谢伙计! 您好,如何使用本示例获取 ID-- Log.e("Id Of User","Id Of user"+p.getId()); 关于库的可怕文档,但这就是为什么您不能在自己的个人资料中使用 getId() 的原因:code.google.com/p/linkedin-j/issues/detail?id=58。更多关于现场选择器的信息:developer.linkedin.com/documents/field-selectors

以上是关于从 Android 应用程序发布 LinkedIn 消息的主要内容,如果未能解决你的问题,请参考以下文章

LinkedIn android应用程序嵌入式网络浏览器不加载网站

使用移动应用在 Android 中集成 LinkedIn

在 Android 中注销 LinkedIn 会话

如何解决Linkedin在android中的集成

Android中LinkedIn的Oauth 2.0授权

从linkedin检索人员/组织信息