如何从 GoogleCredential 获取访问令牌? [英] How to get access token from GoogleCredential?

查看:63
本文介绍了如何从 GoogleCredential 获取访问令牌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取访问令牌以使用 Google Play Android Developer API,并且我使用 Google API Java 客户端文档示例:

I am trying to get an access token to use the Google Play Android Developer API, and I got this far using the Google API Java Client documentation example:

HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
JsonFactory JSON_FACTORY = new JacksonFactory();

GoogleCredential credential = new GoogleCredential.Builder()
    .setTransport(HTTP_TRANSPORT)
    .setJsonFactory(JSON_FACTORY)
    .setServiceAccountId("...gserviceaccount.com")
    .setServiceAccountScopes("https://www.googleapis.com/auth/androidpublisher")
    .setServiceAccountPrivateKeyFromP12File(keyFile)
    .build();

但是我如何从这个凭证中获取访问令牌?credential.getAccessToken() 返回 null.是我做错了什么,还是遗漏了一些步骤?

But how do I get the access token from this credential? credential.getAccessToken() returns null. Am I doing something wrong, or missing some steps?

推荐答案

明白了.您必须在 credential.getAccessToken() 之前调用 credential.refreshToken().它没有在文档的任何地方说明这一点,但这就是它的作用.

Got it. You have to call credential.refreshToken() before credential.getAccessToken(). It doesn't say this anywhere in the documentation but that's what does it.

credential.refreshToken();
accessToken = credential.getAccessToken();

这篇关于如何从 GoogleCredential 获取访问令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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