![](/img/trans.png)
[英]Is there an official way to authenticate for Google Data API on Android using AccountManager accounts?
[英]Authenticating with Google API using AccountManager
我幾天都在努力解決這個問題。 我正試圖通過Android的AccountManager
使用身份驗證撥打Google日歷。 我使用通常的方法檢索身份驗證令牌:
AccountManager manager = AccountManager.get(this);
String authToken = manager.getAuthToken(account, AUTH_TOKEN_TYPE, true, null, null).getResult().getString(AccountManager.KEY_AUTHTOKEN);
然后,使用該令牌,我創建一個這樣的Calendar
實例:
HttpTransport transport = AndroidHttp.newCompatibleTransport();
JacksonFactory jsonFactory = new JacksonFactory();
GoogleAccessProtectedResource accessProtectedResource = new GoogleAccessProtectedResource(accessToken);
Calendar calendar = Calendar.builder(transport, jsonFactory).setApplicationName("MyApp/1.0").setJsonHttpRequestInitializer(new JsonHttpRequestInitializer() {
@Override
public void initialize(JsonHttpRequest request) {
CalendarRequest calendarRequest = (CalendarRequest) request;
calendarRequest.setKey(API_KEY);
}
}).setHttpRequestInitializer(accessProtectedResource).build();
但是,當我使用它進行API調用時,我收到下面看到的401 Unauthorized
錯誤。 請注意,我已經包含了使過期的auth令牌無效的代碼,因此我不認為這是問題所在。
com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
{
"code" : 401,
"errors" : [ {
"domain" : "global",
"location" : "Authorization",
"locationType" : "header",
"message" : "Invalid Credentials",
"reason" : "authError"
} ],
"message" : "Invalid Credentials"
}
關於我可能做錯什么的任何想法?
是的,這是可能的。 一旦掌握了Google帳戶(如您所述),您只需要從AccountManager請求GData服務的身份驗證令牌。
如果Android設備已經有一個身份驗證令牌(對於您嘗試訪問的特定GData服務),它將返回給您。 如果沒有,AccountManager將請求一個並將其返回給您。 無論哪種方式,您都不需要擔心這一點,因為AccountManager會處理它。
在以下示例中,我使用的是Google Spreadsheets API:
ArrayList<Account> googleAccounts = new ArrayList<Account>();
// Get all accounts
Account[] accounts = accountManager.getAccounts();
for(Account account : accounts) {
// Filter out the Google accounts
if(account.type.compareToIgnoreCase("com.google")) {
googleAccounts.add(account);
}
}
AccountManager accountManager = AccountManager.get(activity);
// Just for the example, I am using the first google account returned.
Account account = googleAccounts.get(0);
// "wise" = Google Spreadheets
AccountManagerFuture<Bundle> amf = accountManager.getAuthToken(account, "wise", null, activity, null, null);
try {
Bundle authTokenBundle = amf.getResult();
String authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN);
// do something with the token
InputStream response = sgc.getFeedAsStream(feedUrl, authToken, null, "2.1");
}
和
請查看google data api中的示例代碼 。 身份驗證后要做的重要事情是調用GoogleHeaders.setGoogleLogin(String)。
我希望這有幫助。
我自己也遇到了類似的問題。 我發現我需要在未知密鑰列表中設置xoauth_requestor_id(參考: http ://www.yenlo.nl/2011/google-calendar-api-v3-and-2lo/)
這對我有用:
com.google.api.services.calendar.Calendar.CalendarList.List list = calendar.calendarList().list();
//Set the requestor id
list.getUnknownKeys().put("xoauth_requestor_id", "user@gmail.com");
在此之后,API調用通過了。
我希望有一個解釋為什么需要請求者ID。 誰能解釋一下?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.