Google api 谷歌云存储:使用HTTP 401服务帐户的可恢复下载
我正在尝试使用服务帐户初始化到Google云存储的可恢复下载。生成的授权令牌是有效的,我已在验证它,并且应用程序具有完全控制权。但是,当我尝试为可恢复下载获取新的URI时,我会收到一个401错误:无效凭据。我做错了什么Google api 谷歌云存储:使用HTTP 401服务帐户的可恢复下载,google-api,google-cloud-storage,google-client,Google Api,Google Cloud Storage,Google Client,我正在尝试使用服务帐户初始化到Google云存储的可恢复下载。生成的授权令牌是有效的,我已在验证它,并且应用程序具有完全控制权。但是,当我尝试为可恢复下载获取新的URI时,我会收到一个401错误:无效凭据。我做错了什么 40 $key = file_get_contents(KEY_FILE); 41 $client->setAssertionCredentials(new Google_AssertionCredentials( 42 SERVICE_ACCOUNT_NAM
40 $key = file_get_contents(KEY_FILE);
41 $client->setAssertionCredentials(new Google_AssertionCredentials(
42 SERVICE_ACCOUNT_NAME,
43 array('https://www.googleapis.com/auth/devstorage.full_control'),
44 $key)
45 );
46
47 $client->setClientId(CLIENT_ID);
48 $service = new Google_StorageService($client);
49 $buckets = $service->buckets;
50 $bucketObj = new Google_Bucket();
51
52 $time = time();
53 $b_name = "test_bucket_"."$time";
54 $bucketObj->setName($b_name);
55 // $response = $buckets->insert('test_project-0001',$bucketObj);
56 $response = $buckets->listBuckets('test_project-0001');
57 $tokenStr = $client->getAccessToken();
58 print "Token String : $tokenStr\n";
59 $token = '';
60 if(preg_match('/access_token\":\"(.*.)\",/', $tokenStr, $matches)) {
61 print "Tken $matches[1] \n";
62 $token = $matches[1];
63 }
64
65
66 $req = new Google_HttpRequest("https://www.googleapis.com/upload/storage/v1beta2/b/test_project-0001_test_bucket_1/o?uploadType=resumable&name=song");
67 $req->setRequestHeaders(array(
68 'Authorization:' => "$token",
69 'X-Upload-Content-Length:' => '4509237'));
74 $req->setRequestMethod('POST');
75 // $req->setPostBody($e_body);
76
77 var_dump($req);
78 $gRest = new Google_REST();
79 $response = $gRest->execute($req);
80 var_dump($response);
81
82 ?>
这给了我以下输出
{
错误:{
错误:[{
域名:全球,
原因:错误,
消息:无效凭据,
位置类型:标题,
地点:授权
}],
代码:401,
消息:无效凭据
}
}
谁能给我指点一下我做错了什么吗?我知道了。我需要发送一个经过身份验证的请求,如下所示:
63 $req->setRequestHeaders(array(
65 'X-Upload-Content-Length:' => '4509237'));
请注意,数组中没有身份验证参数
77 $gcIO = new Google_CurlIO();
78 $response = $gcIO->authenticatedRequest($req);
79 $resp_uri = $response->getResponseHeader('location');
就这样