2016-03-22 22 views
2

OK ...私はGoogleのAPIを使用するのに十分な経験がないと言わざるを得ないので、私はできるだけ詳しく説明しようとします。Google APIから禁止されている403(PHPクライアントライブラリv1を使用)

クラウドストレージのデータを取得するためにPHPを使用する必要があるため、バケットからデータを取得するためにgsUtilを使用してクレデンシャルを試しました。私はデータをつかむためにPHPのライブラリを使用しようとする。しかし、APIは、このコンテンツを私に答えた:

"error": { 
    "errors": [ 
    { 
     "domain": "global", 
     "reason": "forbidden", 
     "message": "Forbidden" 
    } 
    ], 
    "code": 403, 
    "message": "Forbidden" 
} 

それはステップが間違っているかを正確に教えてくれなかったので、私はこのサイトを中心に検索したすべてのものを試してみましたので、同様に見えたが、状況は立っている。

ここは私のGoogle Devの設定です。コンソール:PHPで

Api Manager > Overall > Enabled API: 

(a)Drive API. 

(b)Cloud Storage. 

(c)Cloud Storage JSON API. 

Api Manager > Credentials: 

(a)Api Key/OAuth 2.0 ID/Service Acc. Key are created. 

(b)Server IPs are added to the Api key's accept IP list. 

(c)Service Account have the Editor permission to the Project, service acc key is bind to this account too. 

$email = '<my gmail>'; 
$scope = 'https://www.googleapis.com/auth/cloud-platform'; 
$apiKey = '<my api key>'; 
$oAuthId = '<OAuth ID>'; 
$serviceAcc = '<service account id>@developer.gserviceaccount.com'; 
$keyFileLocation = $_SERVER['DOCUMENT_ROOT']. "/<p12 file>"; 
$bucketId = '<my bucket id>'; 
$list = array(); 

$client = new Google_Client(); 
$client->setApplicationName("gp-api"); 
$client->setDeveloperKey($apiKey); 
$cred = new Google_Auth_AssertionCredentials (
      $serviceAcc, 
      array($scope), 
      file_get_contents($keyFileLocation) 
    ); 

$client->setAssertionCredentials($cred); 

if($client->getAuth()->isAccessTokenExpired()) 
    $client->getAuth()->refreshTokenWithAssertion($cred); 

if($client->getAccessToken()) { 
    $reqUrl = "https://www.googleapis.com/storage/v1/b/$bucketId/o/"; 
    $request = new Google_Http_Request($reqUrl, 'GET', null, null); 
    $httpRequest = $client->getAuth()->authenticatedRequest($request); 
    if ($httpRequest->getResponseHttpCode() == 200) { 
    $objects = json_decode($httpRequest->getResponseBody()); 
    foreach ($objects->items as $object) { 
     $list[] = $object->mediaLink; 
    } 
    } 
    else { 
    echo $httpRequest->getResponseHttpCode(); // This is where I got the 403 
    } 
} 

私が何かを逃した場合は教えてください。

+0

バケットに対するREAD権限を持っていない可能性はありますか?また、$ emailと$ serviceAccの両方を定義していますが、Google_Auth_AssertionCredentialsを作成するときは、未定義の変数$ accountNameを使用します。あなたはそれを他のコードで埋めていますか? –

+0

@Brandon Yarbrough おっと、あなたは私のタイピングエラーです。 xDこれで修正されました。 – Kaninchen

+0

@Brandon Yarbroughしかし、もし私がREAD権限を持っていなければ、バケツを読むためにgsUtilを使っている間に私はエラーを受けていたはずです。しかし、これらの資格を持つ私のgsUtilsは問題なく良好に動作します。 – Kaninchen

答えて

0

問題があります。プログラムで言及したAPIスコープにアクセスする特権を持つユーザーアカウントを偽装する必要があります。

だから、いくつかの追加のコードは次のように追加する必要があります。

$email = '<email account which could access that api>'; 
$scope = 'https://www.googleapis.com/auth/cloud-platform'; 
$apiKey = '<my api key>'; 
$oAuthId = '<OAuth ID>'; 
$serviceAcc = '<service account id>@developer.gserviceaccount.com'; 
$keyFileLocation = $_SERVER['DOCUMENT_ROOT']. "/<p12 file>"; 
$bucketId = '<my bucket id>'; 
$list = array(); 

$client = new Google_Client(); 
$client->setApplicationName("gp-api"); 
$client->setDeveloperKey($apiKey); 
$cred = new Google_Auth_AssertionCredentials (
     $serviceAcc, 
     array($scope), 
     file_get_contents($keyFileLocation), 
     'notasecret', 
     'http://oauth.net/grant_type/jwt/1.0/bearer', 
     $email 
); 

Woooooyaaaaaは、別の問題が解決します!次の問題のために上がる時間。

関連する問題