android - 来自 Android 应用程序的一次性代码的服务器端访问 token

标签 android oauth-2.0 google-api google-api-java-client

当我获得来自 Android 应用程序的一次性代码时,如何获得访问 token ?

更具体地说:

  1. 在为 Android 应用创建配置时,在 Google API 控制台上没有客户端密码。执行此操作时,您将客户端密码设置为什么?有些人建议我将此留空..
  2. 如何制作重定向 uri?

我热衷于使用 google api 来完成这项工作,而不是手动编写请求代码。

这是 com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest

这是我的示例代码:

private void getAccessToken(String oAuthGoogleClientId, String oAuthGoogleClientSecret, String one_time_code) throws IOException {
    HttpTransport TRANSPORT = new NetHttpTransport();
    JacksonFactory JSON_FACTORY = new JacksonFactory();
    GoogleTokenResponse tokenResponse = new GoogleAuthorizationCodeTokenRequest(TRANSPORT, JSON_FACTORY, oAuthGoogleClientId, oAuthGoogleClientSecret, one_time_code, "postmessage").execute();
}

最佳答案

首先,您需要在与 Android 应用相同的项目中为您的 Web 服务器创建一个 OAuth2 客户端。

要获取离线授权代码,您可以使用特殊的“范围”字符串从您的 Android 应用程序调用 GoogleAuthUtil.getToken():
oauth2:server:client_id:<web_client_id>:api_scope:<scope1> <scope2> .

然后您将授权码发送到您的服务器,您的服务器执行 OAuth2 Web Server flow使用 Web 服务器 OAuth2 客户端的 client_id 和 client_secret 来交换刷新/访问 token 的代码。 您可以在 cross client auth document 中找到更多信息。 .

关于android - 来自 Android 应用程序的一次性代码的服务器端访问 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24162574/

相关文章:

如果列表太大,Android 双可扩展 ListView 会隐藏项目

java - 实现一个 Map,其中键是一组不重叠的范围

android - 如何在 Android 中实现一个简单的 TableLayout?

android - 膨胀第二个导航主机时,应用程序崩溃

android - 从 Android 获取 Google oauth 授权 token -返回无效范围/未知错误

perl - 你如何在 Perl 中编写 OAuth2 服务器?

spring-boot - 如何验证来自 OAuth 服务器的 token ?

Python Google Drive API 无法使用 mediaFileUpload 插入媒体文件

php - token 中的段数错误 (OAuth Google Api)

ios - 谷歌登录服务器授权码 nil iOS?