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

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

问题描述

我想获得一个访问令牌使用谷歌Play Android开发者的API,我走到这一步使用的谷歌的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()收益。我做得不对,还是缺少一些步骤?

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天全站免登陆