(403) 请求的身份验证范围不足 - OAuth 范围 Google Clas-s-room API
Posted
技术标签:
【中文标题】(403) 请求的身份验证范围不足 - OAuth 范围 Google Clas-s-room API【英文标题】:(403)Request had insufficient authentication scopes - OAuth scopes Google Clas-s-room API 【发布时间】:2016-04-28 03:45:24 【问题描述】:我是新来的,我不是程序员,所以我需要你的帮助。
我正在测试此处找到的管理教师和学生的说明: https://developers.google.com/clas-s-room/guides/manage-users
我尝试像在 php QuickStart 中一样使用身份验证和范围: https://developers.google.com/clas-s-room/quickstart/php
我成功运行quickstart.php示例,则说明composer安装成功,但我无法运行代码 //enroll the student in the course 由于身份验证范围不足。
错误信息:
致命错误:未捕获的异常“Google_Service_Exception”与 message '调用 POST 时出错 h_ttps://clas-s-room.googleapis.com/v1/courses/559522327/students?enrollmentCode=2j1l21l: (403) 请求的身份验证范围不足。在 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(对象(Google_Http_Request), 对象(Google_Client))#1 [内部函数]: Google_Http_REST::doExecute(Object(Google_Client), 对象(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(对象(Google_Client), 对象(Google_Http_Request))#5 C:\Pr inC:\ProgramData\ComposerSetup\bin\vendor\google\apiclient\src\Google\Http\REST.php 110号线
尝试修改示例代码:
<?php
require __DIR__ . '/vendor/autoload.php';
define('APPLICATION_NAME', 'Clas-s-room API PHP Quickstart');
define('CREDENTIALS_PATH', '~/.credentials/clas-s-room-php-quickstart.json');
define('CLIENT_SECRET_PATH', __DIR__ . '/client_secret.json');
define('SCOPES', implode(' ', array(
Google_Service_Clas-s-room::CLAs-s-rOOM_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_Clas-s-room($client);
//Enroll the student in the course given below
$courseId = '559522327';
$enrollmentCode = '2j1l21l';
$student = new Google_Service_Clas-s-room_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_Clas-s-room 类有关吗?
如果你能帮助我,我将非常感激!
问候
【问题讨论】:
【参考方案1】:您的应用程序请求没有身份验证范围。 访问数据需要范围。根据您的要求选择范围。
以下是 Clas-s-room API 的 OAuth 2.0 范围信息: https://developers.google.com/clas-s-room/guides/auth?hl=en
【讨论】:
【参考方案2】:According to the docs,需要以下范围之一:
https://www.googleapis.com/auth/clas-s-room.rosters
https://www.googleapis.com/auth/clas-s-room.profile.emails
https://www.googleapis.com/auth/clas-s-room.profile.photos
【讨论】:
将近一年后,感谢mjs!!!我直接在代码中设置了每个作用域,而不是在这里使用作曲家依赖:define('SCOPES', implode(' ', array( Google_Service_Clas-s-room::CLAs-s-rOOM_ROSTERS) ));
以上是关于(403) 请求的身份验证范围不足 - OAuth 范围 Google Clas-s-room API的主要内容,如果未能解决你的问题,请参考以下文章
错误 403:“请求的身份验证范围不足”,即使相关范围已激活
Google Calendar API - 请求的身份验证范围不足
403 请求在容器内访问 GCP 上的 Secret 时出现身份验证不足问题