stephencoduor / oauth2-canvaslms
支持 PHP 7.4 及以上版本的 Canvas LMS OAuth 2.0
v1.0.0
2022-11-01 20:31 UTC
Requires
- php: >=7.4
- league/oauth2-client: ^2.0
README
本软件包为 PHP League 的 OAuth 2.0 客户端提供 Canvas LMS OAuth 2.0 支持。
安装
如下要求安装包
composer require stephencoduor/oauth2-canvaslms
使用
与 League 的 OAuth 客户端相同,使用 \stephencoduor\OAuth2\Client\Provider\CanvasLMS
作为提供者。请注意,您应该(并且确实应该)包含一个 purpose
选项参数,并且您需要包含您的 canvasInstanceUrl
。
对于由 Instructure 托管的 Canvas Cloud,您可以在“开发者密钥注册”部分从 http://instructure.github.io/ 请求客户端 ID 和密钥。
对于开源 Canvas 用户,您可以在 Canvas 安装站点管理员账户中生成客户端 ID 和密钥。左侧导航侧边栏上会有一个“开发者密钥”标签页。
一个小示例
use stephencoduor\OAuth2\Client\Provider\CanvasLMS; session_start(); /* anti-fat-finger constant definitions */ define('CODE', 'code'); define('STATE', 'state'); define('STATE_LOCAL', 'oauth2-state'); /* Instantiate the class and pass the required configs ie clientId ,clientSecret, and urls */ $provider = new CanvasLMS([ 'clientId' => '676434567890', 'clientSecret' => 'A8h7dZy6i4QS4GkBqrWUxr9jUdgcZobpVMCEBmOGMNa2D3Ab478A', 'purpose' => 'Application Name here', 'redirectUri' => 'https://' . $_SERVER['SERVER_NAME'] . '/' . $_SERVER['SCRIPT_NAME'], 'canvasInstanceUrl' => 'https://canvas.instructure.com' ]); /* if we don't already have an authorization code, let's get one! */ if (!isset($_GET[CODE])) { $authorizationUrl = $provider->getAuthorizationUrl(); $_SESSION[STATE_LOCAL] = $provider->getState(); header("Location: $authorizationUrl"); exit; /* check that the passed state matches the stored state to mitigate cross-site request forgery attacks */ } elseif (empty($_GET[STATE]) || $_GET[STATE] !== $_SESSION[STATE_LOCAL]) { unset($_SESSION[STATE_LOCAL]); exit('Invalid state'); } else { /* try to get an access token (using our existing code) */ $token = $provider->getAccessToken('authorization_code', [CODE => $_GET[CODE]]); /* do something with that token... (probably not just print to screen, but whatevs...) */ echo $token->getToken(); exit; }