簡體   English   中英

如何獲取Google Cloud Storage的訪問代碼?

[英]How to get Access Code for Google Cloud Storage?

我正在嘗試從AngularJS Web應用程序將圖像上傳到Google雲存儲,並執行同樣的操作,我需要在請求上傳時將訪問代碼放置在Authorization標頭中。 為了獲得訪問令牌,我在服務器端引入了一個用JAVA開發的端點。 我當前獲取訪問令牌的代碼是這樣的

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);

但是在日志中,我得到的是空值。

為什么我要從URL獲取JSON文件,是因為當我將JSON文件放在同一包中時,它引發了java.security.AccessControlException。 因此,為避免我將文件放置在Google驅動器上並生成上面代碼中使用的直接下載鏈接。

我只想要訪問代碼並將其發送到Web應用程序,以便可以啟動上傳。 任何幫助,將不勝感激。

謝謝

您使用的代碼不適用於App Engine實例。 您可以將應用程序的JSON文件放在/war/WEB_INF文件夾中,就像處理任何靜態文件一樣,但是更好的解決方案是使用專門在App Engine上運行的代碼:

private static final AppIdentityService identityService = AppIdentityServiceFactory.getAppIdentityService();

String token = identityService
                .getAccessToken(Arrays.asList("https://www.googleapis.com/auth/devstorage.full_control")).getAccessToken();

我可以通過使用以下課程來獲取憑據。

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 ();
    }
}

並進一步使用該存儲對象將文件上傳到Google Cloud Storage。

如果有人需要有關“如何將文件上傳到Google Cloud Storage?”的完整答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM