2012-11-18 99 views
1

我是Android新手,我正在构建一个应用程序,我想使用本地用户的Google帐户进行身份验证。不幸的是,我已经得到了一些关于Auth 2.0并通过谷歌服务登录的约束。验证Android应用程序

什么是推荐路径进行身份验证(并希望不需要输入登录名)?我尝试了许多我看到的样本,但似乎不赞成使用。

任何示例代码也会非常有帮助。

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

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

谢谢, 克雷格

回答

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; 
    } 
    } 

不知道这是否是最好的,但它似乎是为我工作....