Java – Sign in with Flickr Oauth of the Scribe Library

Sign in with Flickr Oauth of the Scribe Library… here is a solution to the problem.

Sign in with Flickr Oauth of the Scribe Library

I am new to android, please forgive any mistakes.

package net.schwiz.oauth;

import org.json.JSONException;
import org.json.JSONObject;
import org.scribe.builder.ServiceBuilder;
import org.scribe.builder.api.TwitterApi;
import org.scribe.model.OAuthRequest;
import org.scribe.model.Response;
import org.scribe.model.Token;
import org.scribe.model.Verb;
import org.scribe.model.Verifier;
import org.scribe.oauth.OAuthService;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

public class Main extends Activity {

final static String APIKEY = "XXXXXXXXXXXXXXXXXXXX";
    final static String APISECRET = "XXXXXXXXXXXXXXXXXXXX";

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

final TextView textView = (TextView)findViewById(R.id.textview);
        final  WebView webview = (WebView) findViewById(R.id.webview);

if(APIKEY == null || APISECRET == null){
            textView.setText("You must enter your own APIKEY and SECRET to use this demo.  dev.twitter.com");
            webview.setVisibility(View.GONE);
            return;
        }

set up service and get request token as seen on scribe website 
        https://github.com/fernandezpablo85/scribe-java/wiki/Getting-Started
        final OAuthService s = new ServiceBuilder()
        .provider(TwitterApi.class)
        .apiKey(APIKEY)
        .apiSecret(APISECRET)
        .callback(CALLBACK)
        .build();

final Token requestToken = s.getRequestToken();
        final String authURL = s.getAuthorizationUrl(requestToken);

attach WebViewClient to intercept the callback url
        webview.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

check for our custom callback protocol otherwise use default behavior
                if(url.startsWith("oauth")){
                    authorization complete hide webview for now.
                    webview.setVisibility(View.GONE);

Uri uri = Uri.parse(url);
                    String verifier = uri.getQueryParameter("oauth_verifier");
                    Verifier v = new Verifier(verifier);

save this token for practical use.
                    Token accessToken = s.getAccessToken(requestToken, v);

if(uri.getHost().equals("twitter")){
                        OAuthRequest req = new OAuthRequest(Verb.GET, "URL here");
                        s.signRequest(accessToken, req);
                        Response response = req.send();
                        try {
                            JSONObject json = new JSONObject(response.getBody());
                            textView.setText(json.toString(3));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

return true;
                }

return super.shouldOverrideUrlLoading(view, url);
            }
        });

send user to authorization page
        webview.loadUrl(authURL);
    }
}

I

tried this code and it works fine on twitter, but now I want to authenticate the user using Flickr, the above code doesn’t work in that case, if the conditional if(url.startsWith("oauth")) doesn’t work the way I imagined. Please help.

Solution

I’ve found the problem! When I call the method webview.loadUrl(authURL); , the URL of the flickr www.flickr.com changes to m.flickr.com, which triggers the event before any user shouldOverrideUrlLoading login/authentication.

if(authURL.charAt(7)=='w')
{
   authURL=authURL.replaceFirst("www", "m");
} 

Solved my problem.

Related Problems and Solutions