Hello All,
I seem to be have in issue with refreshing my canvas token. My process is set to request the authorization code from then redirects to my program to process that code and request a token to be used and stored. Using this initial token I am able to make requests for enrollments, courses, users etc... if that token was deleted or expired then I refresh it and try again.
Below is my request for my initial token with LMS_KEY and LMS_SECRET being values for my actual information.
<?php
$url = "https://felbry.instructure.com/login/oauth2/token";
$oauth = array( 'client_id' => LMS_KEY,
'client_secret' => LMS_SECRET,
'grant_type' => 'authorization_code',
'redirect_uri' => 'https://stars.trainingmasters.com:82/php/FLMS010.php',
'code' => $code);
$header = array('Authorization: Basic ' . base64_encode(LMS_KEY . LMS_SECRET));
$PostFields = buildAuthorizationHeader($oauth);
$options = array( CURLOPT_HTTPHEADER => $header,
CURLOPT_HEADER => false,
CURLINFO_HEADER_OUT => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $PostFields,
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false);
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
$headerSent = curl_getinfo($feed, CURLINFO_HEADER_OUT );
curl_close($feed);
$Oauth_Data = json_decode($json,true);
$Token = $Oauth_Data['access_token'];
$RefToken = $Oauth_Data['refresh_token'];
?>
I then use this token to make the request for all completed courses:
$url = "https://felbry.instructure.com/api/v1/courses?state=completed";
$header = array('Authorization: Bearer ' . trim($Token));
$options = array( CURLOPT_HTTPHEADER => $header,
CURLOPT_HEADER => false,
CURLINFO_HEADER_OUT => true,
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false);
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
$headerSent = curl_getinfo($feed, CURLINFO_HEADER_OUT );
curl_close($feed);
$Oauth_Data2 = json_decode($json,true);
echo '<pre>'.print_r($Oauth_Data2,true).'</pre>';
When I print the result I get a list of all courses as expected.
Then I configure my program to fake a new token so I can trigger my logic to refresh the token. To refresh the token I use the following request $RefToken is the token collected from the initial request:
<?php
$url = "https://felbry.instructure.com/login/oauth2/token";
$oauth = array( 'client_id' => LMS_KEY,
'client_secret' => LMS_SECRET,
'grant_type' => 'refresh_token',
'refresh_token' => trim($RefToken));
$header = array('Authorization: Basic ' . base64_encode(LMS_KEY . LMS_SECRET));
$PostFields = buildAuthorizationHeader($oauth);
$options = array( CURLOPT_HTTPHEADER => $header,
CURLOPT_HEADER => false,
CURLINFO_HEADER_OUT => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $PostFields,
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false);
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
$headerSent = curl_getinfo($feed, CURLINFO_HEADER_OUT );
curl_close($feed);
$Oauth_Data = json_decode($json,true);
$Token = $Oauth_Data['access_token'];
?>
This Process seems to work since I get a new token but when I attempt to use that new token to make the same courses request as above i receive the following message:
Array
(
[errors] => Array
(
[0] => Array
(
[message] => Invalid access token.
)
)
[error_report_id] => 2259
)
I have printed all my values out before and after all requests and everything is sending what its supposed to. I have used this process for other Oauth authentication with no issues. Any help with this issue would be great.
Thank you.