1

私はアンドロイドが初めてで、ローカルユーザーのグーグルアカウントを使用して認証したいアプリを構築しています。残念ながら、私は Auth 2.0 を見て、Google サービス経由でログインするのに少し苦労しました。

認証するための推奨されるルートは何ですか (ログイン名を入力する必要がないことを願っています)? 見たサンプルの多くを試しましたが、その多くは推奨されていないようです。

サンプルコードも非常に役立ちます。

私はこのチュートリアルを使用していましたが、少し時代遅れであり、今でははるかに簡単になっていると思います.

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

ありがとう、クレイグ

4

1 に答える 1

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 を設定するためのロジックがもう少し必要なためです)。このように見えます(私の処理/クッキーロジックの多くはまだ完了していません)

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

これが最善かどうかはわかりませんが、私にとってはうまくいっているようです....

于 2012-11-18T17:30:07.337 に答える