我正在尝试使用服务帐户初始化可恢复下载到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 ?>
这给了我以下输出 { “错误”:{ “错误”:[{ “域名”:“全球”, “reason”:“authError”, “message”:“凭证无效”, “locationType”:“标题”, “位置”:“授权” }], “代码”:401, “message”:“凭据无效” } }
有人能给我一些关于我做错了什么的指示吗?
答案 0 :(得分: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');
就是那个