使用谷歌云存储API与开发人员密钥(而不是OAuth2) [英] using google cloud storage api with developer key (not OAuth2)

查看:565
本文介绍了使用谷歌云存储API与开发人员密钥(而不是OAuth2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一些控制台命令上传文件到谷歌云。据我所知,无法使用OAuth2,所以我需要使用Api密钥。但没有任何工作。我试过这个

  $ ch = curl_init(); 
$ URL =https://www.googleapis.com/upload/storage/v1/b/temp/o?uploadType=media&name=p11&key='.$this->key;
curl_setopt($ ch,CURLOPT_URL,$ url);
curl_setopt($ ch,CURLOPT_POST,1);
curl_setopt($ ch,CURLOPT_HTTPHEADER,[
'Authorization:key'。$ this-> key
]);
curl_setopt($ ch,CURLOPT_POSTFIELDS,'@ / var / www / test.txt');
$ content = curl_exec($ ch);
var_dump($ content);

出现错误:

<$ p $ {error:{errors:[{domain:global,reason:authError,message:Invalid Credentials,
locationType:header,location:Authorization}],
code:401,message:Invalid Credentials}}
pre>

我试过php-sdk

  $ client = new Google_Client(); 
$ client-> setApplicationName(alasdg);
$ client-> setDeveloperKey($ this-> key);
$ client-> setScopes(['https://www.googleapis.com/auth/devstorage.read_write']);
$ client-> getAuth();

$ storageService = new Google_Service_Storage($ client);
$ buckets = $ storageService-> buckets-> listBuckets('temp');
var_dump($ bucket); exit;

错误是:

 调用GET时出错https://www.googleapis.com/storage/v1/b?project=temp&key=afJwBJWucqvIU:(400)无效的参数。 

我可以上传没有OAuth2的文件吗?

Invalid Credentials 错误意味着您的 Authorization 标头在某些内容中无效方式,并且您需要刷新您的OAuth 2.0访问令牌。鉴于API密钥不是有效的访问令牌,这将解释它。你可能想要构造一个类似于 Authorization:Bearer ya29.xxxxxxxx 的头部,其中ya29等是一个访问令牌。凭证交换由php-sdk完成。



第二个错误是因为很可能没有项目ID为温度。这有点困难,因为您可以为项目分配一个友好名称,但该名称可以在许多项目中共享,并且不是唯一的标识符。您必须使用指定的项目ID,或者,如果您尚未为您的项目指定项目,则可以使用数字项目ID,该项目ID在 https://console.developers.google.com/


I want to upload file to google cloud from some console command. As I understand right, can't do it with OAuth2, so i need to use Api key. But nothing works. I tried this

$ch = curl_init();
$url = 'https://www.googleapis.com/upload/storage/v1/b/temp/o?uploadType=media&name=p11&key='.$this->key;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Authorization: key '.$this->key
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, '@/var/www/test.txt');
$content = curl_exec($ch);
var_dump($content);

There is a error:

{ "error": { "errors": [ { "domain": "global", "reason": "authError", "message": "Invalid Credentials", 
  "locationType": "header", "location": "Authorization" } ],
  "code": 401, "message": "Invalid Credentials" } }

And i tried php-sdk

$client = new Google_Client();
$client->setApplicationName("alasdg");
$client->setDeveloperKey($this->key);
$client->setScopes(['https://www.googleapis.com/auth/devstorage.read_write']);
$client->getAuth();

$storageService = new Google_Service_Storage($client);
$buckets = $storageService->buckets->listBuckets('temp');
var_dump($buckets);exit;

The error is:

Error calling GET https://www.googleapis.com/storage/v1/b?project=temp&key=afJwBJWucqvIU: (400) Invalid argument.

Can i upload files without OAuth2?

解决方案

The Invalid Credentials error implies that your Authorization header is invalid in some manner, and you need to refresh your OAuth 2.0 access token. Given that the API key is not a valid access token, that would explain it. You probably want to construct a header that looks like Authorization: Bearer ya29.xxxxxxxx where the "ya29" etc. is an access token. The credential exchange is done on your behalf by the php-sdk.

The second error you are getting because it is very likely that there is no project whose Project ID is "temp". It's a bit a of a gotcha, because you can assign a "friendly name" to your project, but that name can be shared among many projects and is not a unique identifier. You must either use the named Project ID, or, if you haven't assigned your project one, you can use the numeric Project ID, which is visible in the URL when you view it in https://console.developers.google.com/

这篇关于使用谷歌云存储API与开发人员密钥(而不是OAuth2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆