I am trying to authenticate to Google API in a backend service using a service account. It will not have a UI at all.
I have generated private keys in JSON format from Google Developer Console, using a Service Account
but there is no field named "client_secret" in it.
I found this example on GitHub, and its structure should be correctly parsed by GoogleClientSecrets
class.
What are the steps to generate the correct client_secret.json
as in the GitHub example?
I need to do this again after a long time, and I am documenting the steps as of May 2022.
Login to Google Cloud Console
Go to API Service -> Credentials
Click "+ Create Credentials", ans select Service Account
You will be back on Credentials screen. Click the Service account Email you just created.
You can then use the credential to access Google services. For example, in my case I access YouTube service with the following code ( clientSecretsStream
is the InputStream
of that credentials JSON file):
public static YouTube initializeYouTube(InputStream clientSecretsStream, String appName) throws IOException {
final HttpTransport httpTransport = new NetHttpTransport();
final JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
GoogleCredential credential = GoogleCredential.fromStream(clientSecretsStream)
.createScoped(Collections.singleton(YouTubeScopes.YOUTUBE_READONLY));
return new YouTube.Builder(httpTransport, jsonFactory, credential)
.setApplicationName(appName)
.build();
}