|
| 1 | +package com.google.api.services.samples.youtube.cmdline; |
| 2 | + |
| 3 | +import com.google.api.client.auth.oauth2.Credential; |
| 4 | +import com.google.api.client.auth.oauth2.StoredCredential; |
| 5 | +import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp; |
| 6 | +import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver; |
| 7 | +import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; |
| 8 | +import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets; |
| 9 | +import com.google.api.client.http.HttpTransport; |
| 10 | +import com.google.api.client.http.javanet.NetHttpTransport; |
| 11 | +import com.google.api.client.json.JsonFactory; |
| 12 | +import com.google.api.client.json.jackson2.JacksonFactory; |
| 13 | +import com.google.api.client.util.store.DataStore; |
| 14 | +import com.google.api.client.util.store.FileDataStoreFactory; |
| 15 | + |
| 16 | +import java.io.File; |
| 17 | +import java.io.IOException; |
| 18 | +import java.io.InputStreamReader; |
| 19 | +import java.io.Reader; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +/** |
| 23 | + * Shared class used by every sample. Contains methods for authorizing a user and caching credentials. |
| 24 | + */ |
| 25 | +public class Auth { |
| 26 | + |
| 27 | + /** |
| 28 | + * Define a global instance of the HTTP transport. |
| 29 | + */ |
| 30 | + public static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport(); |
| 31 | + |
| 32 | + /** |
| 33 | + * Define a global instance of the JSON factory. |
| 34 | + */ |
| 35 | + public static final JsonFactory JSON_FACTORY = new JacksonFactory(); |
| 36 | + |
| 37 | + /** |
| 38 | + * This is the directory that will be used under the user's home directory where OAuth tokens will be stored. |
| 39 | + */ |
| 40 | + private static final String CREDENTIALS_DIRECTORY = ".oauth-credentials"; |
| 41 | + |
| 42 | + /** |
| 43 | + * Authorizes the installed application to access user's protected data. |
| 44 | + * |
| 45 | + * @param scopes list of scopes needed to run youtube upload. |
| 46 | + * @param credentialDatastore name of the credential datastore to cache OAuth tokens |
| 47 | + */ |
| 48 | + public static Credential authorize(List<String> scopes, String credentialDatastore) throws IOException { |
| 49 | + |
| 50 | + // Load client secrets. |
| 51 | + Reader clientSecretReader = new InputStreamReader(Auth.class.getResourceAsStream("/client_secrets.json")); |
| 52 | + GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, clientSecretReader); |
| 53 | + |
| 54 | + // Checks that the defaults have been replaced (Default = "Enter X here"). |
| 55 | + if (clientSecrets.getDetails().getClientId().startsWith("Enter") |
| 56 | + || clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) { |
| 57 | + System.out.println( |
| 58 | + "Enter Client ID and Secret from https://code.google.com/apis/console/?api=youtube" |
| 59 | + + "into src/main/resources/client_secrets.json"); |
| 60 | + System.exit(1); |
| 61 | + } |
| 62 | + |
| 63 | + // This creates the credentials datastore at ~/.oauth-credentials/${credentialDatastore} |
| 64 | + FileDataStoreFactory fileDataStoreFactory = new FileDataStoreFactory(new File(System.getProperty("user.home") + "/" + CREDENTIALS_DIRECTORY)); |
| 65 | + DataStore<StoredCredential> datastore = fileDataStoreFactory.getDataStore(credentialDatastore); |
| 66 | + |
| 67 | + GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( |
| 68 | + HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, scopes).setCredentialDataStore(datastore) |
| 69 | + .build(); |
| 70 | + |
| 71 | + // Build the local server and bind it to port 8080 |
| 72 | + LocalServerReceiver localReceiver = new LocalServerReceiver.Builder().setPort(8080).build(); |
| 73 | + |
| 74 | + // Authorize. |
| 75 | + return new AuthorizationCodeInstalledApp(flow, localReceiver).authorize("user"); |
| 76 | + } |
| 77 | +} |
0 commit comments