Umesh Chauhan Umesh Chauhan - 3 months ago 48
Java Question

How to get Access Code for Google Cloud Storage?

I am trying to upload a image to google cloud storage from my AngularJS web app and to do the same i need the access code which is to be placed in Authorization header while requesting the upload. To get access token i had introduced an endpoint at server side which is developed in JAVA. My current code to get access token is like this

GoogleCredential credential = GoogleCredential.fromStream(new URL("HERE GOES URL OF MY SERVICE ACCOUNT JSON FILE").openStream());
if (credential.createScopedRequired()) {
Collection<String> scopes = StorageScopes.all();
credential = credential.createScoped(scopes);
}
String token = credential.getAccessToken();
log.log( Level.SEVERE, "5 "+token);


But in logs i am getting null.

Why i am getting the JSON file from a URL is because when i put the JSON file in same package it raised java.security.AccessControlException. So, to avoid that i placed the file on google drive and generated the direct download link which is used in above code.

I just want the access code and send it to web app so that upload can be initiated. Any help would be appreciated.

Thank You

Answer

I am able to get credentials by using following class.

package XXXXXXXXXXXX;

import java.io.IOException;
import java.net.URL;
import java.security.GeneralSecurityException;
import java.util.Collection;

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.storage.Storage;
import com.google.api.services.storage.StorageScopes;

//@author Umesh Chauhan 

public class StorageFactory
{

    private static Storage instance = null;

    public static synchronized Storage getService () throws IOException, GeneralSecurityException
    {
        if ( instance == null )
        {
            instance = buildService ();
        }
        return instance;
    }

    private static Storage buildService () throws IOException, GeneralSecurityException
    {

        HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport ();
        JsonFactory jsonFactory = new JacksonFactory ();

        GoogleCredential credential = GoogleCredential.fromStream (
                new URL ( "HERE GOES THE URL FOR YOUR SERVICE ACCOUNT JSON - I USED GOOGLE DRIVE DIRECT DOWNLOAD LINK TO MY JSON FILE" )
                        .openStream () );

        // Depending on the environment that provides the default credentials
        // (for
        // example: Compute Engine, App Engine), the credentials may require us
        // to
        // specify the scopes we need explicitly. Check for this case, and
        // inject
        // the Cloud Storage scope if required.
        if ( credential.createScopedRequired () )
        {
            Collection<String> scopes = StorageScopes.all ();
            credential = credential.createScoped ( scopes );
        }

        return new Storage.Builder ( transport, jsonFactory, credential ).setApplicationName ( "YOUR PROJECT NAME" ).build ();
    }
}

and further used that Storage object to Upload file to Google Cloud Storage.

If any one need complete answer on "How to upload file to Google Cloud Storage ?"