我正在尝试使用服务帐户初始化到谷歌云存储的可恢复下载。生成的授权令牌有效(我已在https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=)并且应用程序具有完全控制权。然而,当我试图为可恢复下载获取新的URI时,我会收到401错误(无效凭据)。我在这里做错了什么?
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 : $tokenStrn";
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 ?>
这给了我以下输出{"错误":{"错误":[{"域":"全局","原因":"authError","message":"无效凭据","locationType":"标头","位置":"授权"}],"代码":401,"message":"无效凭据"}}
有人能告诉我我做错了什么吗?
我想明白了。事实证明,我需要发送这样一个经过身份验证的请求:
63 $req->setRequestHeaders(array(
65 'X-Upload-Content-Length:' => '4509237'));
(注意数组中没有Authentication参数)
77 $gcIO = new Google_CurlIO();
78 $response = $gcIO->authenticatedRequest($req);
79 $resp_uri = $response->getResponseHeader('location');
这就是