Last active
August 29, 2015 13:56
-
-
Save yashk/9226613 to your computer and use it in GitHub Desktop.
StorageSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Main class for the Cloud Storage API command line sample. | |
* Demonstrates how to make an authenticated API call using OAuth 2 helper classes. | |
*/ | |
public class StorageSample { | |
/** | |
* Be sure to specify the name of your application. If the application name is {@code null} or | |
* blank, the application will log a warning. Suggested format is "MyCompany-ProductName/1.0". | |
*/ | |
private static final String APPLICATION_NAME = "MyCompnay/1.0"; | |
/** Directory to store user credentials. */ | |
private static final java.io.File DATA_STORE_DIR = | |
new java.io.File(System.getProperty("user.home"), ".store/storage_sample"); | |
/** | |
* Global instance of the {@link DataStoreFactory}. The best practice is to make it a single | |
* globally shared instance across your application. | |
*/ | |
private static FileDataStoreFactory dataStoreFactory; | |
/** Global instance of the JSON factory. */ | |
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); | |
/** Global instance of the HTTP transport. */ | |
private static HttpTransport httpTransport; | |
@SuppressWarnings("unused") | |
private static Storage client; | |
/** Authorizes the installed application to access user's protected data. */ | |
private static Credential authorize() throws Exception { | |
// load client secrets | |
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, | |
new InputStreamReader(StorageSample.class.getResourceAsStream("/client_secrets.json"))); | |
if (clientSecrets.getDetails().getClientId().startsWith("Enter") || | |
clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) { | |
System.out.println( | |
"Overwrite the src/main/resources/client_secrets.json file with the client secrets file " | |
+ "you downloaded from the Quickstart tool or manually enter your Client ID and Secret " | |
+ "from https://code.google.com/apis/console/?api=storage#project:<project_id> " | |
+ "into src/main/resources/client_secrets.json"); | |
System.exit(1); | |
} | |
// Set up authorization code flow. | |
// Ask for only the permissions you need. Asking for more permissions will | |
// reduce the number of users who finish the process for giving you access | |
// to their accounts. It will also increase the amount of effort you will | |
// have to spend explaining to users what you are doing with their data. | |
// Here we are listing all of the available scopes. You should remove scopes | |
// that you are not actually using. | |
Set<String> scopes = new HashSet<String>(); | |
scopes.add(StorageScopes.DEVSTORAGE_FULL_CONTROL); | |
scopes.add(StorageScopes.DEVSTORAGE_READ_ONLY); | |
scopes.add(StorageScopes.DEVSTORAGE_READ_WRITE); | |
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( | |
httpTransport, JSON_FACTORY, clientSecrets, scopes) | |
.setDataStoreFactory(dataStoreFactory) | |
.build(); | |
// authorize | |
return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user"); | |
} | |
public static void main(String[] args) { | |
try { | |
// initialize the transport | |
httpTransport = GoogleNetHttpTransport.newTrustedTransport(); | |
// initialize the data store factory | |
dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR); | |
// authorization | |
Credential credential = authorize(); | |
// set up global Storage instance | |
client = new Storage.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME).build(); | |
client.objects().list("<bucket_id>").executeAndDownloadTo(System.out); | |
} catch (IOException e) { | |
System.err.println(e.getMessage()); | |
} catch (Throwable t) { | |
t.printStackTrace(); | |
} | |
System.exit(1); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment