2016-03-22 11 views
2

Tamam ... Google'ın API'sini kullanma konusunda yeterli tecrübem yok. Bu yüzden, olabildiğince ayrıntılı bir şekilde açıklamaya çalışacağım.403'ü kullanmaya devam edin Google API'den yasak (PHP istemci kitaplığı v1'i kullanarak)

Cloud depolama verilerini toplamak için PHP kullanmalıyım, bu yüzden gsUtil ile kimlik bilgimi denedim; Verileri kapmak için PHP kütüphanesi kullanmaya çalıştığınızda Ancak, API, bu içerik ile beni yanıtladı: Yanlış olan adım tam olarak bana söylemedin beri

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

, bu yüzden bu site etrafında arandı ve her şeyi denedi hangi benzer görünüyordu, ancak Durum duruyor.

İşte Google Devrimdeki Yapılandırma. Konsol: 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. 

: Ben bir şey kaçırmış eğer

$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 
    } 
} 

söyle.

+0

Kepçe üzerinde READ izniniz yok mu? Alternatif olarak, hem $ e-posta adresini hem de $ serviceAcc değerini tanımladığınızı görüyorum, ancak Google_Auth_AssertionCredentials oluşturulurken tanımlanmamış bir değişken olan $ accountName kullanıyorsunuz. Bunu başka bir kod parçasıyla mı dolduruyorsunuz? –

+0

@Brandon Yarbrough oops, yazım hatası var. xD Şimdi düzeltildi. – Kaninchen

+0

@Brandon Yarbrough ama READ iznine sahip değilsem, gsUtil'i kullanıp Kepümümü okumak için Hatayı almalıydım. Ama bu kimlik bilgileri ile gsUtils sorun olmadan iyi çalışıyor. – Kaninchen

cevap

0

Tamam, Sorunum var: Programda bahsettiğim API kapsamına erişme ayrıcalığına sahip olan Kullanıcı hesabının kimliğine bürünmelidir.

Yani bazı ek kodlar şu şekilde eklenmesi gerekir:

$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, başka bir sorun çözüldü! Bir sonraki problem için yukarı çıkma zamanı.

İlgili konular