使用的AccountManager谷歌API鉴定 [英] Authenticating with Google API using AccountManager

查看:143
本文介绍了使用的AccountManager谷歌API鉴定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力奋斗着这个几天了。我试图让通过Android的的AccountManager 使用认证调用谷歌日历。我用通常的方法获取一个身份验证令牌:

I've been struggling with this for a couple days now. I'm trying to make calls to Google Calendar using authentication via Android's AccountManager. I retrieve an auth token using the usual method:

AccountManager manager = AccountManager.get(this);
String authToken = manager.getAuthToken(account, AUTH_TOKEN_TYPE, true, null, null).getResult().getString(AccountManager.KEY_AUTHTOKEN);

然后,与道理,我创建了一个日历实例是这样的:

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未授权错误。请注意,我已经包括code无效过期身份验证令牌,所以我不相信这是这里的问题。

However, when I make API calls using this, I receive the 401 Unauthorized error seen below. Note that I have included code to invalidate expired auth tokens, so I don't believe that's the problem here.

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

这是什么,我可能是做错了什么想法?

Any thoughts on what I might be doing wrong?

推荐答案

是的,这是可能的。一旦你对谷歌帐户的句柄(如你所述),你只需要请求身份验证令牌从的AccountManager的GData的服务。

Yes this is possible. Once you have a handle on the Google account (as you described), you just need to request an auth token from the AccountManager for the GData service.

如果Android设备已经有一个身份验证令牌(您试图访问特定的GData服务),它将被退还给您。如果没有,会的AccountManager要求之一,它回报给你。无论哪种方式,你不需要担心这个作为的AccountManager处理它。

If the android device already has an auth token (for the particular GData service you're trying to access), it will be returned to you. If not, the AccountManager will request one and return it to you. Either way, you don't need to worry about this as the AccountManager handles it.

在下面的例子中,我使用的是谷歌小号preadsheets API:

In the following example, I am using the 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");

}

请看看在样品code 在谷歌数据API。认证后做最重要的事情就是打电话给GoogleHeaders.setGoogleLogin(字符串)。

Please have a look at the sample code in the google data api. The important thing to do after authentication is to call GoogleHeaders.setGoogleLogin(String).

我希望这有助于。

这篇关于使用的AccountManager谷歌API鉴定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆