我要問,什麼是手機上已經配置了谷歌帳戶驗證用戶的最佳方式。 我都在看着ClientLogin的第一,但它不是我想要的,因爲它要求用戶在我的應用程序輸入登錄/密碼。 我可以用OAuth做到這一點,但我應該讓用戶在瀏覽器中輸入他的憑據,這也是不可取的。 我想使用的AccountManager(我觀察到它包含特定帳戶的方法getPassword
但它並沒有爲我工作),如果有可能使用某種方式的AccountManager +的ClientLogin或+的AccountManager OAuth的請告訴我。在結果,我希望有能力與用戶谷歌帳號登錄不問他的登錄/密碼(與確認過程的Android設備上),我不知道這是可能的,但如果存在某種方式來實現這個請點我怎麼樣。 ..的Android:谷歌認證
回答
從feb2010一個計算器問題: Anyway to Authenticate a user using Google Apps in an Android application?
和文章的博客文章: http://javagwt.blogspot.com/2009/12/authenticating-android-app-to-google.html
記住,現在的區別是,谷歌確實兩步認證,現在,所以你可能需要修改一些步驟。
我已經閱讀過這個...更具體地說,我明白如何使用OAuth機制來完成此操作,該機制是與其他服務(如Twitter)進行身份驗證的好方法,但Android帳戶已支持Google帳戶,並且我想省略用戶應輸入其憑據並離開的那一步只是確認我的應用程序將使用他的帳戶來檢索數據...還有什麼...我計劃不是從應用程序引擎中檢索數據,而是從谷歌任務中檢索數據 – endryha 2010-11-17 10:51:21
您可以使用的AccountManager編譯:
Account[] accounts = accountManager.getAccountsByType("com.google");
然後提取您從返回的列表中選擇所需的帳戶。 下一步將打電話給AccountManager.getAuthToken
。
一旦你的的authToken,您可以通過調用傳遞給谷歌的API:
api.setUserToken(token);
,而不是要求用戶和密碼setUserCredentials。
是的,您可以訪問accountManager中的信息並使用註冊的Google帳戶(例如)驗證用戶身份,而無需輸入密碼。
下面的代碼是從實例功能混搭通過nick johnson和tencent
開始與你的主要活動:
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>
使用尼克·約翰遜的佈局文件,您可以下載here
當我嘗試此操作時,出現getResult()不能從這一行在主線程上運行:Bundle authTokenBundle = accountManagerFuture.getResult(); 我正在弄清楚爲什麼以及如何解決這個問題。 – 2013-02-22 08:20:29
檢查在Android SDK中的「權威性」示例項目,它正是這麼做的。如果你投票給你的問題一個正確的答案
- 1. 谷歌認證
- 2. 谷歌認證的困難
- 3. 谷歌認證的AspNetCore.Identity
- 4. 更改谷歌默認的認證重定向 - C#(谷歌liblary)
- 5. 谷歌分析API認證
- 6. 沒有Passport.js谷歌+認證
- 7. xcode谷歌認證swift 3
- 8. 谷歌Gmail API - 認證
- 9. 谷歌認證服務
- 10. 谷歌跨域認證
- 11. 身份驗證Android版谷歌火力認證
- 12. Omniauth認證的路線谷歌
- 13. Django的谷歌,OAuth認證錯誤
- 14. 谷歌雲的PCI-DSS認證
- 15. 谷歌OAuth2認證和PHP上的API
- 16. 谷歌阿比.NET庫的OAuth2認證
- 17. 如何保存谷歌認證的會話之間的android
- 18. Facebook的SSO,Android和認證對谷歌的App Engine
- 19. 谷歌加上通過社交認證在Android的集成?
- 20. 的Android,AWS Cognito谷歌OAuth認證使用CognitoCachingCredentialsProvider
- 21. 谷歌地圖的Android API認證問題
- 22. 默認的谷歌地圖在Android的
- 23. 直接點authRoute到谷歌認證
- 24. 谷歌驅動器Java認證
- 25. 谷歌PHP API認證例外
- 26. 網站谷歌認證與火力
- 27. 谷歌翻譯API認證錯誤
- 28. 谷歌播放服務認證(oauth 2.0)
- 29. Gdata python谷歌應用程序認證
- 30. 谷歌分析測量協議認證?
將是很好。或者,如果不符合您的需求,請對它們進行評論。使我們,答覆者(當時您已做出評論)對我們爲幫助您的努力而感到讚賞。 :) – 2012-12-08 13:28:17