Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: LinkedIn OAuth Callback not Working

Tags:

android

oauth

I'm using LinkedIn OAuth in my Android app. I've an LinkedIn application, consumer key and secret already, an as a result I can request succesfully.

Everything is fine until callback. The web page not calling back, I mean onNewIntent or onResume methods not calling. Web page only shows callback url with parameters. I mean it looks like:

callback_url://?oauth_token=324sdf&oath_verifier=as21dsf

Here is my all code:

try {
    consumer = new CommonsHttpOAuthConsumer("ConsumerKey", "ConsumerSecret");
provider = new CommonsHttpOAuthProvider("https://api.linkedin.com/uas/oauth/requestToken", "https://api.linkedin.com/uas/oauth/accessToken", "https://api.linkedin.com/uas/oauth/authorize");
    final String url = provider.retrieveRequestToken(consumer, Constants.OAUTH_CALLBACK_URL);
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)).setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_FROM_BACKGROUND);
    startActivity(intent);                          
} catch (Exception e) {
    e.printStackTrace();
}

Activity is already defined as singleInstance in Manifest.

What's wrong or missing?

like image 214
Martin Avatar asked Jul 25 '12 12:07

Martin


1 Answers

I've found the answer myself after a long research.

I've changed my base class to linkedin-j which can be looked at here.

Then set this constans as below:

    public static final String CONSUMER_KEY = "ConsumerKey";
    public static final String CONSUMER_SECRET = "ConsumerSecret";
    public static final String OAUTH_CALLBACK_SCHEME = "callback";
    public static final String OAUTH_CALLBACK_URL = OAUTH_CALLBACK_SCHEME + ":///";

And initialize like that:

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

liToken = oAuthService.getOAuthRequestToken(Constants.OAUTH_CALLBACK_URL);
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(liToken.getAuthorizationUrl()));
startActivity(i);

This callbacks well and I've handled at OnNewIntent:

String verifier = intent.getData().getQueryParameter("oauth_verifier");

LinkedInAccessToken accessToken = oAuthService.getOAuthAccessToken(liToken, verifier);
client = factory.createLinkedInApiClient(accessToken);
client.postNetworkUpdate("Test");

That's all.

like image 194
Martin Avatar answered Oct 16 '22 08:10

Martin