Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
454 views
in Technique[技术] by (71.8m points)

shopify app - How to generate refresh/access token using google API for 1 hour shopping feed

I am using Google API to generate an access token but this Google video say token will expire in 1 hour. Currently, the token is expired in 4-5 seconds.

How do I generate a token for 1 hour or increase the expiration time for the token also I am using google's authorized process like signing using google and get access token one time but when I make another request then the token is expired.

So please provide valuable feedback.

question from:https://stackoverflow.com/questions/66057359/how-to-generate-refresh-access-token-using-google-api-for-1-hour-shopping-feed

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here is the link of authentication google content api - https://developers.google.com/shopping-content/guides/how-tos/authorizing If you only want access token you can do - tokenResponse.getAccessToken(), It is working for 1 hr and you have to set clientId,clientSecret, redirectUri,and 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: 
" + authorizeUrl + "&prompt=login" + '
')

  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()
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...