1

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();

1 Answer 1

1

The access token needs to be set first before attempting to access a resource. I have adjusted your code to set the token.

<?php
session_start();
require_once '../Internal/oauth.google.php';

$client->setAccessToken($_SESSION["token"]); //set the access token is what you missed

/*$client->getAccessToken() should return a none NULL value if set properly, something we have added above*/
if ($client->getAccessToken() !==NULL) { 
    //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) {
        print_r($course);
        $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 {header('Location: ../index.php'); exit();}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.