验证Android应用程序

时间:2012-11-18 05:28:36

标签: android authentication oauth-2.0

我是Android的新手,我正在构建一个我想使用本地用户google帐户进行身份验证的应用。不幸的是,我已经让自己陷入了严峻考验Auth 2.0并通过谷歌服务登录。

建议的身份验证途径是什么(希望不要求输入登录名)?我尝试了很多我看过的样本,但其中大部分都被弃用了。

任何示例代码也都非常有用。

我正在使用这个教程,但它有点过时,我相信它现在更加简单。

http://blog.notdot.net/2010/05/Authenticating-against-App-Engine-from-an-Android-app

谢谢, 克雷格

1 个答案:

答案 0 :(得分:0)

以下是我如何解决它。不知道这是否是推荐的方法,但它有效...

在我的录入活动(主要)的OnCreate中我放了......

 AccountManager accountManager = AccountManager.get(this);
 Account[] accounts = accountManager.getAccountsByType("com.google");
 AccountManagerFuture<Bundle> futur;
 futur = accountManager.getAuthToken(accounts[0],AUTH_TOKEN_TYPE_USERINFO_PROFILE, null, null,
                new OnTokenAcquired(), new Handler(new OnError()));

在同一项活动中,我创造了......

private class OnTokenAcquired implements AccountManagerCallback<Bundle> {
        @Override
        public void run(AccountManagerFuture<Bundle> result) {
            // Get the result of the operation from the AccountManagerFuture.
            Bundle bundle;
            try {
                bundle = result.getResult();
                // The token is a named value in the bundle. The name of the
                // value
                // is stored in the constant AccountManager.KEY_AUTHTOKEN.
                String token = bundle.getString(AccountManager.KEY_AUTHTOKEN);
                //If token isn't null then let them in and also make sure Crunchy accounts are created
                if(token!=null){
                    ProcessToken pt = new ProcessToken(token);
                    pt.execute("");
                    }

                Intent launch = (Intent) bundle.get(AccountManager.KEY_INTENT);
                if (launch != null) {
                    startActivityForResult(launch, 0);
                    return;
                }
            }catch (OperationCanceledException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (AuthenticatorException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

我还创建了一个asyncTask来处理令牌(因为我做了一些逻辑来设置帐户并设置一个cookie)。它看起来像这样(我的大部分处理/ cookie逻辑尚未完成)

      package com.craig.activities.login;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.os.AsyncTask;
import android.util.Log;

public class ProcessToken extends AsyncTask<String,Integer,Long>{

    private static final String AUTH_ACCESS_TOKEN_URL = "https://www.googleapis.com/oauth2/v1/userinfo?access_token=";
    private static final String DEBUG_TAG = "OnTokenAcquired.class";
    private static String token="";

    public ProcessToken(String tokenValue){
        token=tokenValue;
    }

    @Override
    protected Long doInBackground(String... params) {
        try {
            URL url = new URL(AUTH_ACCESS_TOKEN_URL+token);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            int serverCode= con.getResponseCode();
            if(serverCode==200){
                Log.i(DEBUG_TAG, "code 200!!!");
                                //PUT MY LOGIC IN HERE....
                }
            else{
                Log.i(DEBUG_TAG, "Oops, We had an error on authentication");
            }
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }
     }

不确定这是否是最好的,但它似乎对我有用....