pitstudio / oauth-4-laravel
Laravel 4 的 OAuth 服务提供者
Requires
- php: >=5.3
- lusitanian/oauth: ~0.3
Requires (Dev)
This package is not auto-updated.
Last update: 2024-09-24 09:36:33 UTC
README
oauth-4-laravel 是一个简单的 Laravel 4 服务提供者(包装器),用于 Lusitanian/PHPoAuthLib,它为 PHP 5.3+ 提供OAuth支持,并且非常容易与任何需要OAuth客户端的项目集成。
支持的服务
该库支持 OAuth 1.x 和 OAuth 2.0 兼容的服务。以下是可以找到的当前实现的服务列表。还将很快实现更多服务。
包含的服务实现
- OAuth1
- BitBucket
- Etsy
- FitBit
- Flickr
- Scoop.it!
- Tumblr
- Yahoo
- OAuth2
- Amazon
- BitLy
- Box
- Dailymotion
- Dropbox
- Foursquare
- GitHub
- Harvest
- Heroku
- Mailchimp
- Microsoft
- PayPal
- RunKeeper
- SoundCloud
- Vkontakte
- Yammer
- 更多即将到来!
要了解更多关于 Lusitanian/PHPoAuthLib 的信息,请点击此处
安装
将 oauth-4-laravel 添加到 composer.json 文件中
"require": {
"artdarek/oauth-4-laravel": "dev-master"
}
使用 composer 安装此包。
$ composer update
注册包
在 app/config/app.php
中的 providers
数组中注册服务提供者
'providers' => array( // ... 'Artdarek\OAuth\OAuthServiceProvider' )
在 app/config/app.php
中的 aliases
数组中添加别名
'aliases' => array( // ... 'OAuth' => 'Artdarek\OAuth\Facade\OAuth', )
配置
有两种配置 oauth-4-laravel 的方法。您可以选择最适合您的方法。您可以使用通过 artisan 命令生成的包配置文件(选项 1),或者您可以在 app/config
目录中简单地创建一个名为 oauth-4-laravel.php
的配置文件(选项 2)。
选项 1
使用 artisan 命令创建包配置文件
$ php artisan config:publish artdarek/oauth-4-laravel
选项 2
在配置目录 app/config/oauth-4-laravel.php
中手动创建配置文件,并放置以下代码。
<?php return array( /* |-------------------------------------------------------------------------- | oAuth Config |-------------------------------------------------------------------------- */ /** * Storage */ 'storage' => 'Session', /** * Consumers */ 'consumers' => array( /** * Facebook */ 'Facebook' => array( 'client_id' => '', 'client_secret' => '', 'scope' => array(), ), ) );
凭证
将您的凭证添加到 app/config/packages/artdarek/oauth-4-laravel/config.php
或 app/config/oauth-4-laravel.php
(具体取决于您选择的配置选项)
Storage
属性是可选的,默认为 Session
。其他 选项。
使用
基本使用
只需按照以下步骤操作,您就可以获得一个 服务类对象,该对象遵循以下规则
$fb = OAuth::consumer('Facebook');
可选地,添加一个第二个参数,包含服务需要重定向到的 URL,否则它将重定向到当前 URL。
$fb = OAuth::consumer('Facebook','http://url.to.redirect.to');
使用示例
配置:将您的 Facebook 凭证添加到 app/config/packages/artdarek/oauth-4-laravel/config.php
'Facebook' => array( 'client_id' => 'Your Facebook client ID', 'client_secret' => 'Your Facebook Client Secret', 'scope' => array('email','read_friendlists','user_online_presence'), ),
在您的控制器中使用以下代码
/** * Login user with facebook * * @return void */ public function loginWithFacebook() { // get data from input $code = Input::get( 'code' ); // get fb service $fb = OAuth::consumer( 'Facebook' ); // check if code is valid // if code is provided get user data and sign in if ( !empty( $code ) ) { // This was a callback request from facebook, get the token $token = $fb->requestAccessToken( $code ); // Send a request with it $result = json_decode( $fb->request( '/me' ), true ); $message = 'Your unique facebook user id is: ' . $result['id'] . ' and your name is ' . $result['name']; echo $message. "<br/>"; //Var_dump //display whole array(). dd($result); } // if not ask for permission first else { // get fb authorization $url = $fb->getAuthorizationUri(); // return to facebook login url return Redirect::to( (string)$url ); } }
配置:将您的 Google 凭证添加到 app/config/packages/artdarek/oauth-4-laravel/config.php
'Google' => array( 'client_id' => 'Your Google client ID', 'client_secret' => 'Your Google Client Secret', 'scope' => array('userinfo_email', 'userinfo_profile'), ),
在您的控制器中使用以下代码
public function loginWithGoogle() { // get data from input $code = Input::get( 'code' ); // get google service $googleService = OAuth::consumer( 'Google' ); // check if code is valid // if code is provided get user data and sign in if ( !empty( $code ) ) { // This was a callback request from google, get the token $token = $googleService->requestAccessToken( $code ); // Send a request with it $result = json_decode( $googleService->request( 'https://www.googleapis.com/oauth2/v1/userinfo' ), true ); $message = 'Your unique Google user id is: ' . $result['id'] . ' and your name is ' . $result['name']; echo $message. "<br/>"; //Var_dump //display whole array(). dd($result); } // if not ask for permission first else { // get googleService authorization $url = $googleService->getAuthorizationUri(); // return to google login url return Redirect::to( (string)$url ); } }
配置:将您的 Twitter 凭证添加到 app/config/packages/artdarek/oauth-4-laravel/config.php
'Twitter' => array( 'client_id' => 'Your Twitter client ID', 'client_secret' => 'Your Twitter Client Secret', // No scope - oauth1 doesn't need scope ),
在您的控制器中使用以下代码
public function loginWithTwitter() { // get data from input $token = Input::get( 'oauth_token' ); $verify = Input::get( 'oauth_verifier' ); // get twitter service $tw = OAuth::consumer( 'Twitter' ); // check if code is valid // if code is provided get user data and sign in if ( !empty( $token ) && !empty( $verify ) ) { // This was a callback request from twitter, get the token $token = $tw->requestAccessToken( $token, $verify ); // Send a request with it $result = json_decode( $tw->request( 'account/verify_credentials.json' ), true ); $message = 'Your unique Twitter user id is: ' . $result['id'] . ' and your name is ' . $result['name']; echo $message. "<br/>"; //Var_dump //display whole array(). dd($result); } // if not ask for permission first else { // get request token $reqToken = $tw->requestRequestToken(); // get Authorization Uri sending the request token $url = $tw->getAuthorizationUri(array('oauth_token' => $reqToken->getRequestToken())); // return to twitter login url return Redirect::to( (string)$url ); } }
配置:将您的 Linkedin 凭证添加到 app/config/packages/artdarek/oauth-4-laravel/config.php
'Linkedin' => array( 'client_id' => 'Your Linkedin API ID', 'client_secret' => 'Your Linkedin API Secret', ),
在您的控制器中使用以下代码
public function loginWithLinkedin() { // get data from input $code = Input::get( 'code' ); $linkedinService = OAuth::consumer( 'Linkedin' ); if ( !empty( $code ) ) { // This was a callback request from linkedin, get the token $token = $linkedinService->requestAccessToken( $code ); // Send a request with it. Please note that XML is the default format. $result = json_decode($linkedinService->request('/people/~?format=json'), true); // Show some of the resultant data echo 'Your linkedin first name is ' . $result['firstName'] . ' and your last name is ' . $result['lastName']; //Var_dump //display whole array(). dd($result); }// if not ask for permission first else { // get linkedinService authorization $url = $linkedinService->getAuthorizationUri(array('state'=>'DCEEFWF45453sdffef424')); // return to linkedin login url return Redirect::to( (string)$url ); } }
更多使用示例
示例请参考此处