Mez*_*Mez 11 java google-api google-authentication google-api-java-client google-cloud-storage
我已成功自动完成将Google Big Query中的数据移至Google存储空间的流程.现在,我还需要以自动方式将数据从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();
Run Code Online (Sandbox Code Playgroud)
我的授权码是
/** 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");
}
Run Code Online (Sandbox Code Playgroud)
以下常量的位置
在身份验证/授权过程中我错过了什么?我正进入(状态
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>
Run Code Online (Sandbox Code Playgroud)
寻找答案的其他人.要使用应用程序默认凭据,您需要遵循以下几个步骤...您可以按照以下步骤操作,或查看此链接.
配置系统变量的命令......
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从您的谷歌存储桶中获取对象,例如以下内容
// 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);
| 归档时间: |
|
| 查看次数: |
1086 次 |
| 最近记录: |