0

我正在尝试使用服务帐户将可恢复下载初始化到 Google 云存储。生成的授权令牌是有效的(我已经在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 : $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 ?>

这给了我以下输出 { "error": { "errors": [{ "domain": "global", "reason": "authError", "message": "Invalid Credentials", "locationType": "header" , "location": "Authorization" }], "code": 401, "message": "Invalid Credentials" } }

谁能给我一些关于我做错了什么的指示?

4

1 回答 1

0

我想到了。原来我需要像这样发送一个经过身份验证的请求:

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');

就是这样

于 2013-12-15T04:52:00.757 回答