2016-11-01 17 views
1

トークン/ refreshTokenを作成しようとしています。私のウェブサイトはエンドユーザーに権限を要求せずにGoogleスプレッドシートにデータを送信できます。数時間今..PHP - GoogleスプレッドシートAPI OAuth2-トークンを取得できません

私はWeb + Googleのドキュメントで見つけたいくつかの進歩を遂げましたが、私はそれを働かせることができず、私は何が欠けているのか分かりません。

この時点では、エラーは表示されません(ログにも表示されません)。また、アプリケーションを許可するためのリダイレクトまたは新しいウィンドウも表示されません。

<?php 
    session_start(); 

    require_once('php-google-oauth/Google_Client.php'); 
    include_once('lib/autoload.php'); 
    include_once('php-google-oauth/auth/Google_OAuth2.php'); 

    $CLIENT_ID = 'xxxxxxxxxxxx.apps.googleusercontent.com'; 
    $SECRET = 'xxxxxxxxxxxxxxxxxxxxxxxx'; 
    $REDIRECT = 'http://mywebsite.com'; 
    $APIKEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; 
    // $KEY = file_get_contents('php-google-oauth/client_secret.json'); 

    $client = new Google_Client(); 
    $client->setApplicationName("Contacts to Google Sheets"); 
    $client->setClientId($CLIENT_ID); 
    $client->setClientSecret($SECRET); 
    $client->setRedirectUri($REDIRECT); 
    $client->setScopes(array('https://spreadsheets.google.com/feeds')); 
    $client->setAccessType('offline'); // Gets us our refresh token 

    // Step 1: The user has not authenticated so we give them a link to login 
    if (!$client->getAccessToken() && !isset($_SESSION['token'])) { 
     $authUrl = $client->createAuthUrl(); 
    } 

    // Step 2: The user accepted your access now you need to exchange it. 
    if (isset($_GET['code'])) { 
     $client->authenticate($_GET['code']); 
     $_SESSION['token'] = $client->getAccessToken(); 
     header('Location: ' . filter_var($REDIRECT, FILTER_SANITIZE_URL)); 
    } 

    // Step 3: We have access we can now create our service 
    if (isset($_SESSION['token'])) { 
     $client->setAccessToken($_SESSION['token']); 
     $token = $client->getAccessToken(); 
    } 

    echo '<script>console.log("TOKEN: '. $token .'");</script>'; 
?> 

ありがとうございます!

+0

からトークンを得るのですか/ google-api-php-client)をご覧ください。コードを適切に設定すれば、ガイドとして役立ちます。また、シートAPIの承認リクエストについては、この[ドキュメント](https://developers.google.com/sheets/guides/authorizing)を必ずお読みください。もう1つのことは、デベロッパーコンソールのスプレッドシートAPIをこのAPIにアクセスできるようにする必要があることです。詳細については、[スレッド](https://github.com/asimlqt/php-google-spreadsheet-client/issues/20)をご確認ください。 – KENdi

答えて

0

私は私のコードを見てthis-

// Step 1: The user has not authenticated so we give them a link to login 
if (!$client->getAccessToken() && !isset($_SESSION['token'])) { 
    $authUrl = $client->createAuthUrl(); 
    echo '<a href="'. $authUrl . '">Click here</a>'; 
} 

を試してみましたが、今私はhttps://github.com/google(これを確認するには、[Githubの]を試してみてくださいバックグーグル

関連する問題