我已经成功地自动化了将数据从 Google Big Query 移动到 Google Storage 的过程。现在我还需要以自动方式将数据从 Google Storage 下载到我的环境中。
我正在尝试执行普通的 HTTP 请求,但之前已授权。所以我的 HTTP 请求是
HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory(authorize());
GenericUrl url = new GenericUrl(uri);
HttpRequest request = requestFactory.buildGetRequest(url);
HttpResponse response = request.execute();
String content = response.parseAsString();
我的授权码是
/** Authorizes the installed application to access user's protected data. */
private static Credential authorize() throws Exception
{
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
// load client secrets
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
new InputStreamReader(BigQueryConsumer.class.getResourceAsStream("/secret.json")));
// This creates the credentials datastore at ~/.oauth-credentials/${credentialDatastore}
FileDataStoreFactory fileDataStoreFactory = new FileDataStoreFactory(new File(System.getProperty("user.home") + "/" + CREDENTIALS_DIRECTORY));
// set up authorization code flow
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
httpTransport, JSON_FACTORY, clientSecrets,
SCOPES).setDataStoreFactory(fileDataStoreFactory)
.build();
// authorize
return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
}
下面的常量在哪里
- CREDENTIALS_DIRECTORY:“.oauth-credentials”
- JSON_FACTORY : JacksonFactory.getDefaultInstance()
- SCOPES:一个字符串列表,只有“https://www.googleapis.com/auth/devstorage.full_control”
- HTTP_TRANSPORT : 新的 NetHttpTransport()
我在身份验证/授权过程中遗漏了什么?我得到了
Exception in thread "main" com.google.api.client.http.HttpResponseException: 401 Unauthorized
<HTML>
<HEAD>
<TITLE>Unauthorized</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Unauthorized</H1>
<H2>Error 401</H2>
</BODY>
</HTML>
最佳答案
对于其他寻找答案的人。您需要执行几个步骤才能使用应用程序默认凭据...您可以执行以下步骤,或查看此 link .
- 首要任务:安装谷歌云存储 SDK
- 确保您可以从 SDK 运行命令。如果您没有安装 python,则需要安装 python 2.7 或更高版本,以及 pyopenssl...
- 您需要在 SDK 中通过运行 gcloud auth activate-service-account [服务账户邮箱] --key-fil e [.p12 文件](编辑方括号中的值)。当你运行它时,你应该会收到一条消息,告诉你你已经激活了你的服务帐户
- 您需要通过设置从 SDK 设置环境变量 GOOGLE_APPLICATION_CREDENTIALS 到 secret 的 JSON 路径(从开发人员控制台创建的服务帐户下载的那个) CLOUDSDK_PYTHON_SITEPACKAGES 为 1,同时设置项目
配置系统变量的命令...
set GOOGLE_APPLICATION_CREDENTIALS "secret.json path" set CLOUDSDK_PYTHON_SITEPACKAGES 1 gcloud config set project "your project name"
在您验证并授权自己之后,您就可以开始使用应用程序的默认凭据,前提是您已经正确设置了您的环境。
当您成功设置您的环境后,您可以通过以下方式进行身份验证并获取默认凭据
GoogleCredential credential = GoogleCredential.getApplicationDefault(); if (credential.createScopedRequired()) { credential = credential.createScoped(StorageScopes.all()); } HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); storageService = new Storage.Builder(httpTransport, jsonFactory, credential) .setApplicationName(applicationName).build();
并使用 HTTP GET 从您的 Google 存储桶中获取对象,即类似于以下内容
// Set up and execute a Google Cloud Storage request. String uri = "https://storage.googleapis.com/" + URLEncoder.encode("[your bucket name here]", "UTF-8") + "/" + googleStorageFileName + "/" + fileName; httpTransport = GoogleNetHttpTransport.newTrustedTransport(); HttpRequestFactory requestFactory = httpTransport.createRequestFactory( credential); GenericUrl url = new GenericUrl(uri); HttpRequest request = requestFactory.buildGetRequest(url); HttpResponse response = request.execute(); String content = response.parseAsString(); BufferedWriter writer = new BufferedWriter( new FileWriter( pathToSave + googleStorageFileName)); writer.write( content);
关于java - 使用 Java 从 Google Storage 下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33175993/