我希望通过一些控制台命令将文件上传到谷歌云。据我的理解,不能使用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上传文件吗?