如何在Android上的LinkedIn上发布消息

时间:2013-05-13 07:38:12

标签: android linkedin-j

我在我的应用程序中使用了Selvin的代码..但没有得到如何在墙上发布消息.. 这是我的代码.. 我已在此处 Posting LinkedIn message from Android application

引用此链接
package pl.osadkowski.LITest;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
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 = "6oj6vol2hva6";
    static final String CONSUMER_SECRET = "rreH5PzHDgzXZMpq";

    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() {
        final LinkedInRequestToken liToken = oAuthService
                .getOAuthRequestToken(OAUTH_CALLBACK_URL);
        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);
    }

    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) {
                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();
                showCurrentUser(accessToken);
            } 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);
        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)
        // /////////////////////////////////////////////////////////
            tv.setText(p.getLastName() + ", " + p.getFirstName());
        } catch (LinkedInApiClientException ex) {
            clearTokens();
            Toast.makeText(
                    this,
                    "Appliaction down due LinkedInApiClientException: "
                            + ex.getMessage()
                            + " Authokens cleared - try run application again.",
                    Toast.LENGTH_LONG).show();
            finish();
        }

    }

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

plz为我提供java和xml代码......提前致谢

3 个答案:

答案 0 :(得分:3)

就像你在finishAuthenticate()方法中获得accessToken一样,编写以下代码以在任何AsyncTask的doInBackground方法中发布更新。

LinkedInApiClient client = factory.createLinkedInApiClient(accessToken);

client.postNetworkUpdate(YOUR_MESSAGE_STRING);

简单,不是吗?

答案 1 :(得分:1)

1.您可以从http://socialauth-android.googlecode.com/files/socialauth-android-sdk-3.0.zip下载源代码。

2.转到socialauth-android-sdk-3.0 - >示例,现在导入共享按钮项目。

3.现在回到socialauth-android-sdk-3.0 - > src,现在导入socialauth-android作为共享按钮项目的库。

4.运行共享按钮项目。

答案 2 :(得分:0)

关注Android LinkedIn OAuth实施 http://www.sourcetricks.com/2012/07/android-linkedin-oauth-implementation.html

你也可以从这里下载示例项目..