I neeed 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
- Fill in service account name, it will create a default account id
- Click "Create and Continue"
- In the role selection screen, I selected owner as this was my personal project. If your service will be accessed by external parties, consider giving only required permissions
- Click Continue
- I did not select any user/admin role on screen 3. Click Done.
You will be back on Credentials screen. Click the Service account Email you just created.
- You should be on the Details tab. Click on the KEYS tab.
- Click "Add Key" dropdown, and click "Create New Key".
- Select JSON key type (default), and click create.
- This should download a json file to you. That file
You can then use the credential to access a Google service. 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();
}