2016-04-27 123 views
0

我有以下代碼,可以獲取郵件列表等信息......但是,當我嘗試創建新郵件(用戶)時,出現以下錯誤,希望你能幫助我,我試圖找到更多的信息,但只找到舊的API。這是錯誤 - >:http://i.stack.imgur.com/ZBoqa.png使用API​​創建用戶的錯誤Google Admin-sdk

C:\xampp\php>php -f "c:\Users\Eldelaguila77\Desktop\proyectocorreos\quickstart1.php" 

Error calling POST https://www.googleapis.com/admin/directory/v1/users: (403) Insufficient Permission 

這是我的代碼

<?php 
    require __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_secret1.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) 
     Google_Service_Directory::ADMIN_DIRECTORY_USER) 
    )); 

    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->setAuthConfigFile(CLIENT_SECRET_PATH); 
     $client->setAccessType('offline'); 

     // Load previously authorized credentials from a file. 
     $credentialsPath = expandHomeDirectory(CREDENTIALS_PATH); 
     if (file_exists($credentialsPath)) { 
     $accessToken = file_get_contents($credentialsPath); 
     } 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->authenticate($authCode); 

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

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

    /** 
    * Expands the home directory alias '~' to the full path. 
    * @param string $path the path to expand. 
    * @return string the expanded path. 
    */ 
    function expandHomeDirectory($path) { 
     $homeDirectory = getenv('HOME'); 
     if (empty($homeDirectory)) { 
     $homeDirectory = getenv("HOMEDRIVE") . getenv("HOMEPATH"); 
     } 
     return str_replace('~', realpath($homeDirectory), $path); 
    } 

    // Get the API client and construct the service object. 
    $client = getClient(); 
    $service = new Google_Service_Directory($client); 

    //enviar creacion de usuario 
    $user = new Google_Service_Directory_User(); 
    $name = new Google_Service_Directory_UserName(); 
    // SET THE ATTRIBUTES 
    $name->setGivenName('pruebanueve'); 
    $name->setFamilyName('Testerton'); 
    $user->setName($name); 
    $user->setHashFunction("MD5"); 
    $user->setPrimaryEmail("[email protected]"); 
    $user->setPassword(hash("md5","holamundo")); 


    try 
    { 
     $createUserResult = $service -> users -> insert($user); 
     var_dump($createUserResult); 
    } 
    catch (Google_IO_Exception $gioe) 
    { 
     echo "Error in connection: ".$gioe->getMessage(); 
    } 
    catch (Google_Service_Exception $gse) 
    { 
     echo $gse->getMessage(); 
    } 

    //print $results; 


    // Print the first 10 users in the domain. 
    /*$optParams = array(
     'customer' => 'my_customer', 
     'maxResults' => 10, 
     'orderBy' => 'email', 
    ); 
    $results = $service->users->listUsers($optParams); 

    if (count($results->getUsers()) == 0) { 
     print "No users found.\n"; 
    } else { 
     print "Users:\n"; 
     foreach ($results->getUsers() as $user) { 
     printf("%s (%s) (%s) \n", $user->getPrimaryEmail(), 
      $user->getName()->getFullName(), 
      $user->getlastLoginTime()); 
     } 
    }*/ 
    ?> 

回答

0

create a user account使用您的領域之一,請使用以下POST請求,包括授權描述授權要求。

POST https://www.googleapis.com/admin/directory/v1/users 

請注意,您的應用程序發送到Directory API的每個請求都必須包含授權令牌。

+0

我試過$ client-> setScopes(array('https:// www .googleapis.com/admin/directory/v1/users')); 但我得到了同樣的錯誤403,我發送授權信號,請參閱上面的代碼 – Eldelaguila77

0

你的問題幫了我很多。我正在做你正在做的事情,但是試圖手工完成JSON。無論如何,我解決了您的問題:

您必須刪除〜/ .credentials/admin-directory_v1-php-quickstart.json(使用ADMIN_DIRECTORY_USER_READONLY作用域創建它,現在已不夠用了。這個腳本會爲你生成一個新的,然後它就可以工作了(用戶最終被掛起了)然後添加一個

$user->setSuspended(false); 
相關問題