【问题标题】:Flickr Oauth AndroidFlickr Oauth Android
【发布时间】:2015-06-12 10:13:47
【问题描述】:

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

我正在使用 webview、scribe 和 Flickr4Java 作为运行 URL,它们提供代码、身份验证和 Web 视图向我显示代码,它我必须传递给我的应用程序,但我不明白如何从 webview 检索此 code,并传递给 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/#/#

【问题讨论】:

    标签: java android flickr


    【解决方案1】:

    最后,我找到了答案(这提供了一个回调 url: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) {
    
            }
    
        }
    

    【讨论】:

    • 最晦涩的答案通常是最有帮助的:-)。请注意,这需要 Flickr4J 创建请求令牌,因为 scribe 没有提供回调 URL 的功能。是的,我知道现在是 2021 年……但我的项目需要对 flickr 进行身份验证 :-)
    【解决方案2】:

    如果有人正在努力让它与带有 Kotlin 的最新版本的 Android 一起工作,我有一个要点,可以在 here 找到。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多