0

这是基于 selvin 的编码完成的,但它对我不起作用,即消息没有发布在 android 的链接墙上。登录后停留在同一页面。这是代码`public class LITestActivity extends Activity {

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

static final String CONSUMER_KEY = "key";

static final String CONSUMER_SECRET = "secret";

static final String APP_NAME = "abc";
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 = "token secret";
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));
    i.putExtra("sms_body", "Welcome to Rebuix, http://www.rebuix.com");
    startActivity(i);
    Toast.makeText(this,
            "Successfuly posted: " ,
            Toast.LENGTH_LONG).show();
}

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

}`

4

1 回答 1

1

尝试以下 URL 以集成社交网络,如 facebook、Twitter 和 LinkedIn

于 2013-03-15T12:28:25.920 回答