2017-07-04 8 views
-1

PHPを使用してのGmailビジネスユーザーアカウントを新規作成します。このコードを実行すると、URLにOpen the following link in your browser:のようなメッセージが表示され、確認コードを要求します。ブラウザでリンクを実行すると、ランダムな文字列が生成されます。私は403エラーを得て、検証のためにランダムな文字列を端末に貼り付けます。ここに私のコードです。GoogleのAPIを使用してPHPを使用してGmailビジネスユーザーアカウントを作成する方法

<?php 
require_once __DIR__ . '/vendor/autoload.php'; 


define('APPLICATION_NAME', 'Directory API PHP Quickstart'); 
define('CREDENTIALS_PATH', '~/.credentials/admin-directory_v1-php-quickstart.json'); 
define('CLIENT_SECRET_PATH', __DIR__ . '/client_secret.json'); 
// If modifying these scopes, delete your previously saved credentials 
// at ~/.credentials/admin-directory_v1-php-quickstart.json 
define('SCOPES', implode(' ', array(
    Google_Service_Directory::ADMIN_DIRECTORY_USER_READONLY) 
)); 

if (php_sapi_name() != 'cli') { 
    throw new Exception('This application must be run on the command line.'); 
} 

/** 
* Returns an authorized API client. 
* @return Google_Client the authorized client object 
*/ 
function getClient() { 
    $client = new Google_Client(); 
    $client->setApplicationName(APPLICATION_NAME); 
    $client->setScopes(SCOPES); 
    $client->setAuthConfig(CLIENT_SECRET_PATH); 
    $client->setAccessType('offline'); 

    // Load previously authorized credentials from a file. 
    $credentialsPath = expandHomeDirectory(CREDENTIALS_PATH); 
    if (file_exists($credentialsPath)) { 
    $accessToken = json_decode(file_get_contents($credentialsPath), true); 
    } else { 
    // Request authorization from the user. 
    $authUrl = $client->createAuthUrl(); 
    printf("Open the following link in your browser:\n%s\n", $authUrl); 
    print 'Enter verification code: '; 
    $authCode = trim(fgets(STDIN)); 

    // Exchange authorization code for an access token. 
    $accessToken = $client->fetchAccessTokenWithAuthCode($authCode); 

    // Store the credentials to disk. 
    if(!file_exists(dirname($credentialsPath))) { 
     mkdir(dirname($credentialsPath), 0700, true); 
    } 
    file_put_contents($credentialsPath, json_encode($accessToken)); 
    printf("Credentials saved to %s\n", $credentialsPath); 
    } 
    $client->setAccessToken($accessToken); 

    // Refresh the token if it's expired. 
    if ($client->isAccessTokenExpired()) { 
    $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken()); 
    file_put_contents($credentialsPath, json_encode($client->getAccessToken())); 
    } 
    return $client; 
} 

マイエラー

Fatal error: Uncaught Google_Service_Exception: { "error": { "errors": [ { "domain": "global", "reason": "forbidden", "message": "Not Authorized to access this resource/api" } ], "code": 403, "message": "Not Authorized to access this resource/api" } }

+0

スタックへようこそhttps://stackoverflow.com/help/how-to-askを読んでください。あなたの質問は、あなたが試したことを私たちに見せてください、あなたの現在の解決策に伴う問題を説明してください。 – DaImTo

+0

私は自分の質問を更新しました。 –

+0

どの403エラーがあなたの質問に完全なエラーをコピーしてください。 – DaImTo

答えて

0

Fatal error: Uncaught Google_Service_Exception:

{ 
    "error":{ 
     "errors":[ 
     { 
      "domain":"global", 
      "reason":"forbidden", 
      "message":"Not Authorized to access this resource/api" 
     } 
     ], 
     "code":403, 
     "message":"Not Authorized to access this resource/api" 
    } 
} 

あなたが認証しているユーザーがadminディレクトリアカウントにアクセス

The Directory API lets you perform administrative operations on users, groups, organizational units, and devices in your account.

ソリューションを持っていないことを意味:Gスイートアカウントにアクセスできるユーザーでログインしてください。

+0

うんが...それを手に入れた...おかげで.. –

+0

それは今起きている。これはPHPを使用してユーザーアカウントを作成する方法ですか? –

関連する問題