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();
}
If you dont have a client secret then you have generated the wrong type of credentials.
To create a client_secret.json file. You go to Google developer console.
You will then be promoted to download your client_secret.json file.
If you intend to use any of the google apis be sure to go to library on the left and enable the APIs you will be using.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With