java - Flickr Oauth Android

标签 java android flickr

我想从我的应用程序授权用户,我遵循在互联网上找到的一些示例( 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());
}

我使用 webviewscribeFlickr4Java 来运行 URL,它们提供了代码、身份验证,并且 Web View 向我显示了一个代码,其中我必须传递给我的应用程序,但我无法理解如何从 webview 检索此代码,并传递给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

最佳答案

最后,我找到了一个答案(这提供了一个回调网址:token = authInterface.getRequestToken("your calback url");),某人的身份验证代码是:

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) {

        }

    }

关于java - Flickr Oauth Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30800561/

相关文章:

javascript - 为什么会出现分号语法错误?

java - 了解 Spring 3.0 示例中的 Ajax 简化

java - 二维数组循环越界

java - 如何返回 ArrayList <Integer>?

ios - 无法在 flickr ios 上上传视频

Android:在 Flickr 上上传图片

java - 如何使 vector 同步,使得两个线程只有一个线程可以访问它

android - 我怎样才能在android模拟器上上网?

android - 用户登录无法使用改造和 View 模型

android - 带有 EncryptedSharedPreferences 的 AutoBackUp 无法恢复