dtb81443 2016-01-21 18:27
浏览 75
已采纳

(403)请求的身份验证范围不足 - OAuth范围Google Classroom API

I'm new here and I am not a programmer , then I need your help .

I am testing instructions Manage teachers and students found here : https://developers.google.com/classroom/guides/manage-users

I tried to use authentication and scopes as they do here in PHP QuickStart : https://developers.google.com/classroom/quickstart/php

I run successfully quickstart.php sample, then it means that the composer is successfully installed, but I just can not run the code to //Enroll the student in the course due insufficient authentication scopes.

Error message:

Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling POST h_ttps://classroom.googleapis.com/v1/courses/559522327/students?enrollmentCode=2j1l21l: (403) Request had insufficient authentication scopes.' in C:\ProgramData\ComposerSetup\bin\vendor\google\apiclient\src\Google\Http\REST.php:110 Stack trace: #0 C:\ProgramData\ComposerSetup\bin\vendor\google\apiclient\src\Google\Http\REST.php(62): Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request), Object(Google_Client)) #1 [internal function]: Google_Http_REST::doExecute(Object(Google_Client), Object(Google_Http_Request)) #2 C:\ProgramData\ComposerSetup\bin\vendor\google\apiclient\src\Google\Task\Runner.php(174): call_user_func_array(Array, Array) #3 C:\ProgramData\ComposerSetup\bin\vendor\google\apiclient\src\Google\Http\REST.php(46): Google_Task_Runner->run() #4 C:\ProgramData\ComposerSetup\bin\vendor\google\apiclient\src\Google\Client.php(593): Google_Http_REST::execute(Object(Google_Client), Object(Google_Http_Request)) #5 C:\Pr inC:\ProgramData\ComposerSetup\bin\vendor\google\apiclient\src\Google\Http\REST.php on line 110

Attempt to adapt the sample code:

<?php

require __DIR__ . '/vendor/autoload.php';

define('APPLICATION_NAME', 'Classroom API PHP Quickstart');
define('CREDENTIALS_PATH', '~/.credentials/classroom-php-quickstart.json');
define('CLIENT_SECRET_PATH', __DIR__ . '/client_secret.json');
define('SCOPES', implode(' ', array(
  Google_Service_Classroom::CLASSROOM_ROSTERS) 
));

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:
%s
", $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), 0700, true);
    }
    file_put_contents($credentialsPath, $accessToken);
    printf("Credentials saved to %s
", $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_Classroom($client);


//Enroll the student in the course given below

$courseId = '559522327';
$enrollmentCode = '2j1l21l';
$student = new Google_Service_Classroom_Student(array(
  'userId' => 'me',
));

$params = array(
  'enrollmentCode' => $enrollmentCode
);

try {
  $student = $service->courses_students->create($courseId, $student, $params);
  printf("User '%s' was enrolled  as a student in the course with ID '%s'.
",
      $student->profile->name->fullName, $courseId);
} catch (Google_Service_Exception $e) {
  if ($e->getCode() == 409) {
    print "You are already a member of this course.
";
  } else {
    throw $e;
  }
}

?>

It's something to do with scopes and Google_Service_Classroom classes?

I will be very thankful if you can help me!

Regards

  • 写回答

1条回答 默认 最新

  • dtddjq9637 2016-02-02 14:47
    关注

    According to the docs, one of the following scopes is required:

    • https://www.googleapis.com/auth/classroom.rosters
    • https://www.googleapis.com/auth/classroom.profile.emails
    • https://www.googleapis.com/auth/classroom.profile.photos
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料