Flickr Oauth Android

765 views Asked by At

I want to authorize user from my app, I am following some example found on internet (http://www.programcreek.com/java-api-examples/index.php?api=org.scribe.model.Token):

public static void auth() throws IOException, FlickrException {
  Properties properties;
  InputStream in=null;
  try {
    in=AuthExample.class.getResourceAsStream("/setup.properties");
    properties=new Properties();
    properties.load(in);
  }
  finally {
    IOUtilities.close(in);
  }
  Flickr flickr=new Flickr(properties.getProperty("apiKey"),properties.getProperty("secret"),new REST());
  Flickr.debugStream=false;
  AuthInterface authInterface=flickr.getAuthInterface();
  Scanner scanner=new Scanner(System.in);
  Token token=authInterface.getRequestToken();
  System.out.println("token: " + token);
  String url=authInterface.getAuthorizationUrl(token,Permission.READ);
  System.out.println("Follow this URL to authorise yourself on Flickr");
  System.out.println(url);
  System.out.println("Paste in the token it gives you:");
  System.out.print(">>");
  String tokenKey=scanner.nextLine();
  Token requestToken=authInterface.getAccessToken(token,new Verifier(tokenKey));
  System.out.println("Authentication success");
  Auth auth=authInterface.checkToken(requestToken);
  System.out.println("Token: " + requestToken.getToken());
  System.out.println("nsid: " + auth.getUser().getId());
  System.out.println("Realname: " + auth.getUser().getRealName());
  System.out.println("Username: " + auth.getUser().getUsername());
  System.out.println("Permission: " + auth.getPermission().getType());
}

I am using webview, scribe and Flickr4Java for run URL which provide a code, authenticate and web view shows me a code, which I must pass to my app, but I can't understand how to retrieve this code from webview, and pass to tokenKey.

I am added onpageFinished and print URL which me give:
06-12 13:03:55.266     E/NEW﹕ uri is: https://m.flickr.com/services/oauth/authorize?oauth_token=72157654039925698-81abc00d035f5da0&perms=write
06-12 13:03:55.601    W/BindingManager﹕ Cannot call determinedVisibility() - never saw a connection for the pid: 4581
06-12 13:03:56.166      E/NEW﹕ uri is: https://m.flickr.com/#/services/oauth/authorize/_QM_oauth_token_IS_72157654039925698-81abc00d035f5da0_AND_perms_IS_write
06-12 13:03:56.476    W/BindingManager﹕ Cannot call determinedVisibility() - never saw a connection for the pid: 4581
06-12 13:03:56.476    E/NEW﹕ uri is: https://m.flickr.com/#/services/oauth/authorize/_QM_oauth_token_IS_72157654039925698-81abc00d035f5da0_AND_perms_IS_write
06-12 13:04:00.411   W/BindingManager﹕ Cannot call determinedVisibility() - never saw a connection for the pid: 4581
06-12 13:04:00.416   E/NEW﹕ uri is: https://m.flickr.com/#/#

enter image description here

2

There are 2 answers

1
Alexandro On

Finaly, i found an answer (this provide a calback url: token = authInterface.getRequestToken("your calback url");), the code for auth for someone is:

public class FlickrLogin1 extends AsyncTask<String, String, String> {

        public final String TAG = FlickrLogin1.class.getSimpleName();
        String url;
        int count = 0;


        @Override
        protected void onPreExecute() {
            Log.d(TAG, "START");
        }

        @Override
        protected String doInBackground(String... params) {
            String result = "";
            try {

                Flickr.debugRequest = false;
                Flickr.debugStream = false;


                flickr = new Flickr(flickrKey, flickrSecret, new REST());
                authInterface = flickr.getAuthInterface();

                token = authInterface.getRequestToken("your calback url");
                L("Token: " + token);

                result = authInterface.getAuthorizationUrl(token, Permission.WRITE);


                return result;
            } catch (IllegalStateException e) {
                e.printStackTrace();
                return result;
            } catch (VerifyError e) {
                e.printStackTrace();
                return result;
            }

        }

        @Override
        protected void onPostExecute(String result) {
            if (result != null && result.length() > 0) {
                L("Follow this URL to authorise yourself on Flickr");
                L(result);
                auth_dialog = new Dialog(getActivity());
                auth_dialog.setContentView(R.layout.auth_dialog);
                final WebView web = (WebView) auth_dialog.findViewById(R.id.webv);
                web.getSettings().setJavaScriptEnabled(true);

                web.loadUrl(result);

                web.setWebViewClient(
                        new WebViewClient() {

                            @Override
                            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                                super.onPageStarted(view, url, favicon);
                                L("url to start " + url);
                                if (url.contains("&oauth_verifier")) {
                                    auth_dialog.dismiss();
                                    Uri uri = Uri.parse(url);
                                    String oauth_verifier = uri.getQueryParameter("oauth_verifier");
                                    String oauth_token = uri.getQueryParameter("oauth_token");
                                    new FlickrLogin2().execute(oauth_token, oauth_verifier);
                                }
                            }

                            String authCode;

                            @Override
                            public void onPageFinished(WebView view, String url) {
                                super.onPageFinished(view, url);
                                L("url to get " + url);


                            }
                        });

                auth_dialog.show();
                auth_dialog.setTitle("Authorize");
                auth_dialog.setCancelable(true);


            }

        }
    }


    public class FlickrLogin2 extends AsyncTask<String, Void, String> {
        @Override
        protected void onPreExecute() {
        }

        @Override
        protected String doInBackground(String... oauth_verifier) {
            L("CODE " + oauth_verifier[0] + " " + oauth_verifier[1]);
            try {
                Verifier verifier = new Verifier(oauth_verifier[1]);
                Token accessToken = authInterface.getAccessToken(token, verifier);
                System.out.println("Authentication success");
                Auth auth = new Auth();
                authInterface = flickr.getAuthInterface();
                Token requestToken = authInterface.getRequestToken();
                L("auth tocen and secret: " + requestToken.getToken() + " , " + requestToken.getSecret());
                auth.setToken(requestToken.getToken());
                auth.setTokenSecret(requestToken.getSecret()); // thats the token I got from the registration, before I set the token of the requestToken
                auth.setPermission(Permission.WRITE);
                RequestContext requestContext = RequestContext.getRequestContext();
                requestContext.setAuth(auth);
                flickr.setAuth(auth);
                L("checking for token" + accessToken);
                auth = authInterface.checkToken(accessToken);

                // This token can be used until the user revokes it. 
                L("Token: " + accessToken.getToken());
                L("Secret: " + accessToken.getSecret());
                L("nsid: " + auth.getUser().getId());
                L("Realname: " + auth.getUser().getRealName());
                L("Username: " + auth.getUser().getUsername());
                L("Permission: " + auth.getPermission().getType());
            } catch (FlickrException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {

        }

    }
0
Dexter Legaspi On

Anyone who's struggling to get this to work with more recent version of Android with Kotlin, I have a gist that can be found here.