简体   繁体   中英

Flickr Oauth Login using Scribe Library

I'm pretty new to android, please forgive any mistake.

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, its working fine with twitter, but now i want to authenticate user using Flickr, The above code is not working in that case, the if condition if(url.startsWith("oauth")) is not working the way I supposed. Please help.

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

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

solved my problem.

Anyone who's struggling to get this to work with more recent version of Android, I have a gist that can be found here . This uses Scribe and Flickr4J because Scribe does not have a facility AFAIK to create a requestToken for auth with a callback. One of the things to note here is that the code above will not work as is with more recent version of Android since it no longer allow.network tasks in the UI thread...and I addressed this in the gist through the use of (the recently deprecated AsyncTask )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM