安卓:谷歌认证

发布于 2024-10-02 20:58:49 字数 393 浏览 0 评论 0原文

我想问,使用手机上已配置的 Google 帐户对用户进行身份验证的最佳方法是什么。 我首先查看了 ClientLogin,但这不是我想要的,因为它要求用户在我的应用程序中输入登录名/密码。 我可以使用 OAuth 来完成此操作,但我应该要求用户在浏览器中输入他的凭据,这也不是更好的选择。 我想使用 AccountManager (我观察到它包含特定帐户的方法 getPassword 但它对我不起作用),如果可以以某种方式使用 AccountManager + ClientLogin 或 AccountManager + OAuth 告诉我请。结果,我希望能够使用用户 Google 帐户登录,而无需询问他的登录名/密码(当然需要在 Android 设备上进行确认),我不确定这是否可能,但如果存在某种方法来实现这一点,请指出我如何做。 ..

I want to ask, what is the best way to authenticate user with his Google account already configured on the phone.
I have looked at the ClientLogin first, but it's not what I want because it requires users to enter login/password in my application.
I can do it with OAuth but there I should ask user to enter his credentials in browser what is not preferable too.
I want to use AccountManager (I've observed that it contains method getPassword for particular account but it didn't work for me), if it's possible to use somehow AccountManager + ClientLogin or AccountManager + OAuth tell me please. In result I want to have capabilities to log in with user Google account without asking his login/password (with confirmation on android device of course) I am not sure that it's possible but if there exists some way to implement this please point me how...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

轻许诺言 2024-10-09 20:58:49

是的,您可以访问 accountManager 中的信息并使用注册的 google 帐户(例如)对用户进行身份验证,而无需输入密码。

下面的代码是 nick johnson 的示例的功能混合和匹配腾讯

从您的主要活动开始:

    package com.tg.auth;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ZAuth02aActivity extends ListActivity {
    protected AccountManager accountManager;
    protected Intent intent;
    String TAG = "TGtracker";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        accountManager = AccountManager.get(getApplicationContext());
        Account[] accounts = accountManager.getAccountsByType("com.google");
        this.setListAdapter(new ArrayAdapter<Account>(this, R.layout.list_item, accounts));        

    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        Account account = (Account)getListView().getItemAtPosition(position);
        Intent intent = new Intent(this, AppInfo.class);
        intent.putExtra("account", account);
        startActivity(intent);
    }
}

创建一个 AppInfo 类:

    package com.tg.auth;


import org.apache.http.impl.client.DefaultHttpClient;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AccountManagerFuture;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class AppInfo extends Activity {
    DefaultHttpClient http_client = new DefaultHttpClient();
    Activity activity;
    String TAG = "TGtracker";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.app_info);
        activity = this;

    }

    @Override
    protected void onResume() {
        super.onResume();
        //Log.v(TAG, "resuming activity");
        AccountManager accountManager = AccountManager.get(getApplicationContext());
        //if result is null, you might not have a valid internet connection
        Log.i(TAG, "got token, yipee: "+updateToken(accountManager, true));
    }


    private String updateToken(AccountManager am, boolean invalidateToken) {
        String authToken = "null";
        try {
            Account[] accounts = am.getAccountsByType("com.google");
            AccountManagerFuture<Bundle> accountManagerFuture;
            if(activity == null){//this is used when calling from an interval thread
                accountManagerFuture = am.getAuthToken(accounts[0], "android", false, null, null);
            } else {
                accountManagerFuture = am.getAuthToken(accounts[0], "android", null, activity, null, null);
            }
            Bundle authTokenBundle = accountManagerFuture.getResult();
            authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN).toString();
            Log.v(TAG, "newToken preinvalidate: "+authToken);

            if(invalidateToken) {
                am.invalidateAuthToken("com.google", authToken);
                authToken = updateToken(am, false);

            }
        } catch (IOException e) {
            Log.e(TAG, "the exception was: "+e.toString());
            e.printStackTrace();
        }
        return authToken;
    }
}

使用清单文件,有点像这样(必须声明活动+权限):

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tg.auth"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="7" />
    <uses-permission android:name="android.permission.MANAGE_ACCOUNTS"></uses-permission>
    <uses-permission android:name="android.permission.GET_ACCOUNTS"></uses-permission>
    <uses-permission android:name="android.permission.USE_CREDENTIALS"></uses-permission>
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".ZAuth02aActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".AppInfo"></activity>
    </application>

</manifest>

使用您可以下载的 nick johnson 的布局文件 此处

yes, you can access information in the accountManager and authenticate the user using registered google account (for example) without her entering password.

the code below is a functional mix and match from examples by nick johnson and by tencent

start with your main activity:

    package com.tg.auth;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ZAuth02aActivity extends ListActivity {
    protected AccountManager accountManager;
    protected Intent intent;
    String TAG = "TGtracker";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        accountManager = AccountManager.get(getApplicationContext());
        Account[] accounts = accountManager.getAccountsByType("com.google");
        this.setListAdapter(new ArrayAdapter<Account>(this, R.layout.list_item, accounts));        

    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        Account account = (Account)getListView().getItemAtPosition(position);
        Intent intent = new Intent(this, AppInfo.class);
        intent.putExtra("account", account);
        startActivity(intent);
    }
}

create an AppInfo class:

    package com.tg.auth;


import org.apache.http.impl.client.DefaultHttpClient;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AccountManagerFuture;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class AppInfo extends Activity {
    DefaultHttpClient http_client = new DefaultHttpClient();
    Activity activity;
    String TAG = "TGtracker";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.app_info);
        activity = this;

    }

    @Override
    protected void onResume() {
        super.onResume();
        //Log.v(TAG, "resuming activity");
        AccountManager accountManager = AccountManager.get(getApplicationContext());
        //if result is null, you might not have a valid internet connection
        Log.i(TAG, "got token, yipee: "+updateToken(accountManager, true));
    }


    private String updateToken(AccountManager am, boolean invalidateToken) {
        String authToken = "null";
        try {
            Account[] accounts = am.getAccountsByType("com.google");
            AccountManagerFuture<Bundle> accountManagerFuture;
            if(activity == null){//this is used when calling from an interval thread
                accountManagerFuture = am.getAuthToken(accounts[0], "android", false, null, null);
            } else {
                accountManagerFuture = am.getAuthToken(accounts[0], "android", null, activity, null, null);
            }
            Bundle authTokenBundle = accountManagerFuture.getResult();
            authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN).toString();
            Log.v(TAG, "newToken preinvalidate: "+authToken);

            if(invalidateToken) {
                am.invalidateAuthToken("com.google", authToken);
                authToken = updateToken(am, false);

            }
        } catch (IOException e) {
            Log.e(TAG, "the exception was: "+e.toString());
            e.printStackTrace();
        }
        return authToken;
    }
}

use a manifest file, somewhat like this (must declare activities + permissions):

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tg.auth"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="7" />
    <uses-permission android:name="android.permission.MANAGE_ACCOUNTS"></uses-permission>
    <uses-permission android:name="android.permission.GET_ACCOUNTS"></uses-permission>
    <uses-permission android:name="android.permission.USE_CREDENTIALS"></uses-permission>
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".ZAuth02aActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".AppInfo"></activity>
    </application>

</manifest>

use nick johnson's layout files that you can download here

紅太極 2024-10-09 20:58:49

您可以使用AccountManager中的构建:

  Account[] accounts = accountManager.getAccountsByType("com.google");

然后从返回的列表中提取您想要的帐户。
下一步是调用 AccountManager.getAuthToken

获得 authToken 后,您可以通过调用将其传递给 Google API:

api.setUserToken(token);

而不是需要用户和密码的 setUserCredentials。

You can use the build in AccountManager:

  Account[] accounts = accountManager.getAccountsByType("com.google");

Then extract the account you want from the returned list.
The next step would be to call AccountManager.getAuthToken.

Once you have the authToken, you can pass it on to Google APIs by calling:

api.setUserToken(token);

as opposed to the setUserCredentials that require the user and password.

囍笑 2024-10-09 20:58:49

来自 2010 年 2 月的 stackoverflow 问题:
无论如何使用 Google Apps 对用户进行身份验证在 Android 应用程序中?

以及文章博客文章:
http://javagwt.blogspot.com/2009/ 12/authenticating-android-app-to-google.html

请记住,现在的区别是 Google 现在执行两步身份验证,因此您可能需要修改一些步骤。

From a stackoverflow question in feb2010:
Anyway to Authenticate a user using Google Apps in an Android application?

and the article blog post:
http://javagwt.blogspot.com/2009/12/authenticating-android-app-to-google.html

Remember, the difference now is that Google does two-step authentication now so you might have to modify some steps.

我三岁 2024-10-09 20:58:49

检查 Android SDK 中的“auth”示例项目,它正是这样做的。

Check the "auth" sample project in the Android SDK, it does exactly that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文