smtech / oauth-negotiator
v1.1.2
2016-02-15 19:30 UTC
Requires
- battis/educoder-pest-fork: dev-master
This package is auto-updated.
Last update: 2021-12-18 23:35:36 UTC
README
由于推荐使用 smtech/oauth2-canvaslms,因此废弃此包,该包扩展了 PHP League 的优秀 OAuth2 客户端。
OAuthNegotiator
我发现协商 OAuth 令牌很困难,并且我希望不需要去考虑这个问题。这尤其关注与 Instructure Canvas 协商 OAuth 令牌。
使用方法
在您的 composer.json
文件中包含
{ "requires" : { "smtech/oauth-negotiator": "1.0" } }
当您想要获取令牌时,最简单的使用方法是提供三个页面(继续阅读以了解单页面变体)
page1.php
<html> <body> <form action="page2.php" method="post"> <label>Enter the URL of your canvas instance</label> <input type="text" name="url" placeholder="https://canvas.instructure.com" /> <input type="submit" value="Negotiate!" /> </form> </body> </html>
page2.php
由于 page2.php
在协商过程中会被重新加载多次,因此最好将协商的这部分内容放在单独的页面上,并在该页面之间进行重定向。
<?php $oauth = new OAuthNegotiator( $_REQUEST['url'] . '/login/oauth2', '0000000001', // Canvas developer ID '6987c1e292a98deff97c97f2cbc49985', // Canvas developer key/secret (referred to both ways in their documentation) 'page3.php', // where to go when we're done 'OAuthNegotiator' // your purpose for this token (displayed on the user settings page in Canvas) ); ?>
page3.php
<?php $oauth = new OAuthNegotiator(); // get your token echo $oauth->getToken(); // get the user information associated with that token print_r($oauth->getUser()); ?>
### 单页面使用方法
/* attempt to create a simple OAuthNegotiator for the intermediate steps in the workflow */ try { $oauth = new OAuthNegotiator(); } catch (OAuthNegotiator_Exception $e) {} /* otherwise, check what step in the workflow we're at */ if (isset($_REQUEST['oauth'])) { switch ($_REQUEST['oauth']) { case 'request': { // explain what's up to the user echo ' <html> <body> <h1>Token Request</h1> <p>Explain why you’re requesting a token.</p> <p><a href="' . $_SERVER['PHP_SELF'] . '?oauth=process">Click to continue</a></p> </body> </html>'; exit; } case 'process': { // start the negotiation process $oauth = new OAuthNegotiator( "https://canvas.instructure.com/login/oauth2", // replace with your OAuth provider endpoint (string) $secrets->oauth->id, (string) $secrets->oauth->key, "{$_SERVER['PHP_SELF']}?oauth=complete", (string) $secrets->app->name ); break; } case 'complete': { // negotiation is complete /* do something productive with your token */ $_SESSION['apiToken'] = $oauth->getToken(); /* on to the next page, token in hand! */ header("Location: ./next.php"); exit; } } }
完整文档位于包的在线文档中。