I'm having trouble with the Google API, but I'm also brand new to playing around with their API, so I'm needing some help. From what I'm gathering, the $client->getAccessToken is not being set properly. Can someone please help me figure out why the error message keeps reading inside of the gradebook.php? Why will the token not take?
Gradebook.php:
<?php
session_start();
require_once 'oauth.php';
if ($client->getAccessToken()) {
//Now have offline access of the user
$_SESSION['token'] = $client->getAccessToken();
//User token google drive call:
$service = new Google_Service_Classroom($client);
$courses = $service->courses->listCourses(['teacherId' => 'me']);
foreach ($courses as $key => $course) {
// var_dump($course->id);
$courseWorks = $service->courses_courseWork->listCoursesCourseWork($course['id']);
foreach ($courseWorks as $k => $courseWork) {
// print_r($courseWork);
// var_dump($courseWork->id);
$studentSubmissions = $service->courses_courseWork_studentSubmissions->listCoursesCourseWorkStudentSubmissions($course->id, $courseWork->id);
// print_r($studentSubmissions);
foreach ($studentSubmissions as $i => $studentSubmission) {
$student = $service->courses_students->get($course->id, $studentSubmission->userId);
$emailAddress = $student->getProfile()->emailAddress;
$emailId = explode('@', $emailAddress)[0];
var_dump($emailId);
var_dump($course->id);
var_dump($courseWork->id);
var_dump($studentSubmission->assignedGrade);
}
}
}
}
else {echo "ERROR DETECTED!!";}
Oauth.php:
session_start();
require_once '../vendor/autoload.php';
// initation configuration, as specificed by Google
$clientID = '(redacted)';
$clientSecret = '(redacted)';
$redirectUri = '(redacted)';
// create Client Request to access Google API
$client = new Google_Client();
$client->setClientId($clientID);
$client->setClientSecret($clientSecret);
$client->setRedirectUri($redirectUri);
$client->setScopes(array(
"email",
"profile",
"https://www.googleapis.com/auth/classroom.courses.readonly",
"https://www.googleapis.com/auth/classroom.coursework.students.readonly",
"https://www.googleapis.com/auth/classroom.rosters.readonly",
"https://www.googleapis.com/auth/classroom.coursework.students.readonly",
"https://www.googleapis.com/auth/classroom.profile.emails",
"https://www.googleapis.com/auth/classroom.profile.photos"
));
// establish session define_syslog_variables
$client->fetchAccessTokenWithAuthCode($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();