Skip to content

Instantly share code, notes, and snippets.

@GaetanoPiazzolla
Created March 21, 2022 18:03
Show Gist options
  • Save GaetanoPiazzolla/70977da36227fc03ce0e783a4d53efb9 to your computer and use it in GitHub Desktop.
Save GaetanoPiazzolla/70977da36227fc03ce0e783a4d53efb9 to your computer and use it in GitHub Desktop.
Create an Access token from the Service Account key
import com.google.api.services.forms.v1.FormsScopes;
import com.google.auth.oauth2.GoogleCredentials;
import java.io.IOException;
import java.util.Objects;
public static String getAccessToken() throws IOException {
GoogleCredentials credential = GoogleCredentials.fromStream(Objects.requireNonNull(
Main.class.getResourceAsStream("cred.json"))).createScoped(FormsScopes.all());
return credential.getAccessToken() != null ?
credential.getAccessToken().getTokenValue() :
credential.refreshAccessToken().getTokenValue();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment