1

ここから Google API PHP 単純クエリの例を実行しようとしています: https://github.com/google/google-api-php-client/blob/master/examples/service-account.php 私の目標はOAuth2 をテストし、Web アプリと共有されている Google スプレッドシートとの間にサーバー間接続を作成します。

ここに私が持っているものがあります:

$client_id = 'XXX.apps.googleusercontent.com'; //Client ID
    $service_account_name = 'XXX@developer.gserviceaccount.com'; //Email Address
    $key_file_location = 'C:/Users/Hp/Downloads/cMessage-638a8a247351.p12'; 
    if ($client_id == '' || !strlen($service_account_name) || !strlen($key_file_location)) {
        echo missingServiceAccountDetailsWarning();
    }
    $client = new Google_Client();
    $client->setApplicationName("cMessage");
    $service = new Google_Service_Books($client);
    /*       * **********************************************
      If we have an access token, we can carry on.
      Otherwise, we'll get one with the help of an
      assertion credential. In other examples the list
      of scopes was managed by the Client, but here
      we have to list them manually. We also supply
      the service account
     * ********************************************** */
    if (isset($_SESSION['service_token'])) {
        $client->setAccessToken($_SESSION['service_token']);
    }
    $key = file_get_contents($key_file_location);
    $cred = new Google_Auth_AssertionCredentials(
            $service_account_name, array('https://www.googleapis.com/auth/books'), $key
    );
    $client->setAssertionCredentials($cred);
    if ($client->getAuth()->isAccessTokenExpired()) {
        $client->getAuth()->refreshTokenWithAssertion($cred);
    }
    $_SESSION['service_token'] = $client->getAccessToken();
    /*       * **********************************************
      We're just going to make the same call as in the
      simple query as an example.
     * ********************************************** */
    $optParams = array('filter' => 'free-ebooks');
    $results = $service->volumes->listVolumes('Henry David Thoreau', $optParams);
    echo "<h3>Results Of Call:</h3>";
    foreach ($results as $item) {
        echo $item['volumeInfo']['title'], "<br /> \n";
    }

これを実行しようとすると、次のようになります。

GET の呼び出し中にエラーが発生しましたhttps://www.googleapis.com/books/v1/volumes?q=Henry+David+Thoreau&filter=free-ebooks : (403) 権限が不十分です

認証情報とタイムゾーンを再確認し、開発者コンソールで Google の Books API を有効にしました。

他に何が欠けているのかわかりません。助けてください。ありがとう。

4

1 に答える 1

0

わかりました、私はそれを働かせました!誰かがこれに出くわした場合に備えて、私がしたことは次のとおりです。

Google Developer's Console の API & AUTH >> API の下で、いくつかの API と SDK を有効にする必要がありました。どれがうまくいったかはよくわかりませんが、有効にした API と SDK は次のとおりです。

管理者 SDK

BigQuery API

ブックス API

デバッグレット コントローラ API

ドライブ API

ドライブ SDK

Google Apps Marketplace SDK

Google クラウド SQL

Google クラウド ストレージ

Google クラウド ストレージ JSON API

Google+ API

于 2015-02-11T12:41:12.663 に答える