我想通过一些控制台命令将文件上传到Google Cloud。据我所知,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);
发生错误:
{ "error": { "errors": [ { "domain": "global", "reason": "authError", "message": "Invalid Credentials",
"locationType": "header", "location": "Authorization" } ],
"code": 401, "message": "Invalid Credentials" } }
我尝试了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;
错误是:
Error calling GET https://www.googleapis.com/storage/v1/b?project=temp&key=afJwBJWucqvIU: (400) Invalid argument.
我可以在没有OAuth2的情况下上传文件吗?
该Invalid Credentials
错误表明您的Authorization
标头在某种程度上无效,并且您需要刷新OAuth 2.0访问令牌。由于API密钥不是有效的访问令牌,因此可以对其进行解释。您可能想要构造一个看起来像Authorization: Bearer ya29.xxxxxxxx
“ ya29”等作为访问令牌的标头。凭证交换由php-sdk代表您完成。
您收到的第二个错误是因为很可能没有项目ID为“ temp”的项目。这有点麻烦,因为您可以为项目分配一个“友好名称”,但是该名称可以在许多项目之间共享,并且不是唯一的标识符。您必须使用命名的项目ID,或者,如果尚未分配项目名称,则可以使用数字项目ID,当您在https://console.developers.google中查看该URL时,该数字将显示在URL中。 com /
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句