Arne
Arne

Reputation: 79

How to resolve Bad Request on assignments endpoint in classes?

When I call the Graph /assigments endpoint, I get a Bad Request with the following message:

Resource not found for the segment 'assignments'.

This same code pattern works with other endpoints like /teachers and /members, just not /assignments.

Code:

$getEventsUrlAssignments = '/education/classes/' . $class->getId() . '/assignments';
try {
    $assignments = $graph->createRequest('GET', $getEventsUrlAssignments)
        ->setReturnType(Model\EducationAssignment::class)
        ->execute();
} 
catch (\GuzzleHttp\Exception\ClientException $e) {
    dd($e->getResponse()->getBody()->getContents());
}

I've added the right permissions as described in the documentation:

What could I possibly be doing wrong?

Upvotes: 1

Views: 125

Answers (1)

Marc LaFleur
Marc LaFleur

Reputation: 33094

You're receiving this error because /assignments is only available in the Beta version of Graph. If you attempt to call this endpoint using v1.0, Graph will reject the request with the following error:

{
    "error": {
        "code": "BadRequest",
        "message": "Resource not found for the segment 'assignments'.",
        "innerError": {
            "request-id": "38df490c-3a2b-4fe8-a77e-a7cce82831b5",
            "date": "2020-02-20T18:46:16"
        }
    }
}

In order to use the /assignments endpoints, you'll need to call the Beta version:

/beta/education/assignments/

You can tell the SDK to use the Beta version using setApiVersion:

$graph = new Graph();
$graph
  ->setBaseUrl("https://graph.microsoft.com/")
  ->setApiVersion("beta")
  ->setAccessToken($_SESSION['access_token']);

$getEventsUrlAssignments = '/education/classes/' . $class->getId() . '/assignments';
try {
    $assignments = $graph->createRequest('GET', $getEventsUrlAssignments)
        ->setReturnType(Model\EducationAssignment::class)
        ->execute();
} 
catch (\GuzzleHttp\Exception\ClientException $e) {
    dd($e->getResponse()->getBody()->getContents());
}

You'll also need to reference the Beta models since there isn't a model for Assignment in v1.0. This process is described in the SDK's Wiki under Using Beta Models.

Upvotes: 1

Related Questions