私はここに来たばかりで、プログラマーではありません。あなたの助けが必要です。
ここにある教師と生徒の管理の手順をテストしています: https://developers.google.com/classroom/guides/manage-users
ここで PHP QuickStart で行うように、認証とスコープを使用しようとしました: https://developers.google.com/classroom/quickstart/php
これは、composer が正常にインストールされたことを意味しますが、//認証範囲が不十分なため、学生をコースに登録するためのコードを実行できません。
エラーメッセージ:
致命的なエラー: 「POST h_ttps://classroom.googleapis.com/v1/courses/559522327/students?enrollmentCode=2j1l21l の呼び出し中にエラーが発生しました: (403) 要求の認証スコープが不十分でした」というメッセージを含む例外「Google_Service_Exception」がキャッチされませんでした。C:\ProgramData\ComposerSetup\bin\vendor\google\apiclient\src\Google\Http\REST.php:110 スタック トレース: #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 [内部関数]: 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:
サンプル コードを変更してみてください。
<?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:\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), 0700, 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_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'.\n",
$student->profile->name->fullName, $courseId);
} catch (Google_Service_Exception $e) {
if ($e->getCode() == 409) {
print "You are already a member of this course.\n";
} else {
throw $e;
}
}
?>
スコープと Google_Service_Classroom クラスと関係があるのですか?
あなたが私を助けることができれば、私は非常に感謝します!
よろしく