如何使用谷歌 API 为 1 小时购物提要生成刷新/访问令牌

问题描述

我正在使用 Google API 生成访问令牌,但此 Google video 表示令牌将在 1 小时后过期。目前,令牌在 4-5 秒后过期。

我如何生成 1 小时的令牌或增加令牌的到期时间,我也在使用谷歌的授权流程,例如使用谷歌签名并获取访问令牌一次,但是当我发出另一个请求时,令牌已过期。

所以请提供宝贵的反馈。

解决方法

这里是认证google content api的链接 - https://developers.google.com/shopping-content/guides/how-tos/authorizing 如果您只想要访问令牌,您可以这样做 - tokenResponse.getAccessToken(),它工作 1 小时,您必须设置 clientId、clientSecret、redirectUri 和 Scope。

object Authentication extends App {

  import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport
  import com.google.api.client.http.HttpTransport
  import com.google.api.client.json.JsonFactory
  import com.google.api.client.json.jackson2.JacksonFactory

  val httpTransport: HttpTransport = GoogleNetHttpTransport.newTrustedTransport
  val jsonFactory: JsonFactory = JacksonFactory.getDefaultInstance

  import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets.Details

  val details = new Details
  details.setClientId("")
  details.setClientSecret("")

  val redirectUrl = ""
  val scope = ""

  import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets

  val clientSecrets = new GoogleClientSecrets
  clientSecrets.setInstalled(details)

  val authorizationFlow: GoogleAuthorizationCodeFlow = new GoogleAuthorizationCodeFlow.Builder(
    httpTransport,jsonFactory,clientSecrets,Lists.newArrayList(scope)
  ).setAccessType("offline")
    .build()

  val authorizeUrl: String = authorizationFlow.newAuthorizationUrl.setRedirectUri(redirectUrl).build
  System.out.println("Paste this url in your browser: \n" + authorizeUrl + "&prompt=login" + '\n')

  import java.io.BufferedReader
  import java.io.InputStreamReader

  System.out.println("Type the code you received here: ")
  val authorizationCode: String = new BufferedReader(new InputStreamReader(System.in)).readLine

  import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest
  import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse
  // Authorize the OAuth2 token.// Authorize the OAuth2 token.

  val tokenRequest: GoogleAuthorizationCodeTokenRequest =
    authorizationFlow.newTokenRequest(authorizationCode)
  tokenRequest.setRedirectUri(redirectUrl)
  println(s"token Request - $tokenRequest")
  val tokenResponse: GoogleTokenResponse = tokenRequest.execute()
  println(s"token response - $tokenResponse")

  // Create the OAuth2 credential.
  val credential: GoogleCredential = new GoogleCredential.Builder()
    .setTransport(new NetHttpTransport())
    .setJsonFactory(new JacksonFactory())
    .setClientSecrets(clientSecrets)
    .build()

  // Set authorized credentials.
  credential.setFromTokenResponse(tokenResponse)

  val service: ShoppingContent = new ShoppingContent.Builder(httpTransport,credential)
    .setApplicationName("")
    .build()
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...