Question

I want to develop an Android app (ice cream sandwich) that uses the LinkedIn API. For this I use the Scribe library to implement the OAuth process.

My problem is that I dont know how to get the access token from the web view after the user has allowed my LinkedIn App to access his LinkedIn data.

In the web I found a lot of tutorials, but no tutorial explains how to get the token with ice cream sandwhich. What I have read in the web is that I cant create http calls in the UI thread with ice cream sandwich. Therefore I developened an async task to get the authorization url.

In my activity I have a button that has the following OnClickListener:

private OnClickListener createOnClickListener(final SocialAPI socialAPI) {
    return new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            if(PreferencesManager.getToken(AccountsActivity.this, socialAPI) == null) {
                new OAuthRequestTokenAsyncTask(AccountsActivity.this, new AsyncTaskResultHandler<String>() {
                    @Override
                    public void handleResult(String result) {
                        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(result)));
                    }

                    @Override
                    public void onError(Exception e) {
                        //nothing to do here
                    }
                }).execute(socialAPI);
            }
        }
    };
}

The async task makes the following:

protected String doInBackground(SocialAPI... socialAPIs) {
    SocialAPI socialAPI = socialAPIs[0];

    OAuthService oauthService = new ServiceBuilder()
        .provider(socialAPI.apiClass)
        .apiKey(socialAPI.consumerKey)
        .apiSecret(socialAPI.consumerSecret)
        .callback(socialAPI.callbackUrl)
        .build();

    Token requestToken = oauthService.getRequestToken();
    return oauthService.getAuthorizationUrl(requestToken);
}

After the user has entered his credentials in the web view, the original activity is again invoked by callback operation noNewIntent:

public void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    final Uri uri = intent.getData();
    System.out.println("what to do here");
}

And at this position I dont know how to get the access token. I think I have to develop a second async task in which I have to inject the request token (according to scribe documentation), but how to do this from operation onNewIntent...

Verifier verifier = new Verifier("verifier");
Token accessToken = service.getAccessToken(requestToken, verifier);

Btw, if the app executes the http calls in the UI thread, then I get following exception:

org.scribe.exceptions.OAuthConnectionException: There was a problem while creating a connection to the remote service.

Thanks in advance...

Was it helpful?

Solution

Yes, a second async task has to be developed...

public void onNewIntent(Intent intent) {
    super.onNewIntent(intent);

    final Uri uri = intent.getData();
    final SocialAPI socialAPI = SocialAPI.fromScheme(uri.getScheme(), uri.getSchemeSpecificPart());

    new OAuthAccessTokenAsyncTask(this, new AsyncTaskResultHandler<Token>() {
        @Override
        public void handleResult(Token result) {
            PreferencesManager.setAccessToken(AccountsActivity.this, socialAPI, result);
        }

        @Override
        public void onError(Exception e) {
            //Nothing to do here
        }
    }, uri).execute(socialAPI);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top