如何获取 Google 云存储的访问代码?
How to get Access Code for Google Cloud Storage?
我正在尝试从我的 AngularJS 网络应用程序将图像上传到 google 云存储并执行相同的操作,我需要放置在授权中的访问代码 header 请求上传时。为了获得访问令牌,我在 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 驱动器上并生成上面代码中使用的直接下载 link。
我只想要访问代码并将其发送到网络应用程序,以便可以启动上传。任何帮助将不胜感激。
谢谢
您使用的代码不适用于 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();
我可以使用以下 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 ();
}
}
并进一步使用该存储对象将文件上传到 Google 云存储。
如果有人需要
的完整答案
我正在尝试从我的 AngularJS 网络应用程序将图像上传到 google 云存储并执行相同的操作,我需要放置在授权中的访问代码 header 请求上传时。为了获得访问令牌,我在 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 驱动器上并生成上面代码中使用的直接下载 link。
我只想要访问代码并将其发送到网络应用程序,以便可以启动上传。任何帮助将不胜感激。
谢谢
您使用的代码不适用于 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();
我可以使用以下 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 ();
}
}
并进一步使用该存储对象将文件上传到 Google 云存储。
如果有人需要