首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用google API为1小时购物提要生成刷新/访问令牌

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

Stack Overflow用户
提问于 2021-02-05 11:55:17
回答 1查看 66关注 0票数 0

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

我如何生成一个令牌1小时或增加令牌的过期时间,我也在使用谷歌的授权过程,如使用谷歌签名和获取访问令牌一次,但当我提出另一个请求,然后令牌过期。

因此,请提供有价值的反馈。

EN

回答 1

Stack Overflow用户

发布于 2021-02-10 01:35:37

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

代码语言:javascript
运行
复制
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, jsonFactory, credential)
    .setApplicationName("")
    .build()
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66057359

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档