ytokarchukova / oauth-5-laravel
Laravel 5 的 OAuth 服务提供商
Requires
- php: >=5.3
- lusitanian/oauth: ~0.3
Requires (Dev)
README
oauth-5-laravel 是一个简单的 Laravel 5 服务提供商(包装器),用于 Lusitanian/PHPoAuthLib,该库为 PHP 5.4+ 提供OAuth支持,并且很容易与任何需要OAuth客户端的项目集成。
最初由 Artdarek 为 Laravel 4 开发,我将其移植到 Laravel 5。
支持的服务
该库支持 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-5-laravel 添加到您的 composer.json 文件中
"require": {
"oriceon/oauth-5-laravel": "dev-master"
}
使用 composer 安装此包。
$ composer update
注册包
在 config/app.php 中的 providers 数组内注册服务提供者
'providers' => [ // ... Artdarek\OAuth\OAuthServiceProvider::class, ]
在 config/app.php 中的 aliases 数组内添加别名
'aliases' => [ // ... 'OAuth' => Artdarek\OAuth\Facade\OAuth::class, ]
配置
有两种方式来配置 oauth-5-laravel。您可以选择最方便的方式。您可以使用通过 artisan 命令生成的包配置文件(选项 1),或者您可以直接在 config 目录下创建一个名为 oauth-5-laravel.php
的配置文件(选项 2)。
选项 1
使用 artisan 命令为包创建配置文件
$ php artisan vendor:publish --provider="Artdarek\OAuth\OAuthServiceProvider"
选项 2
在 config 目录下手动创建配置文件 config/oauth-5-laravel.php
并放入以下代码。
<?php use OAuth\Common\Storage\Session; return [ /* |-------------------------------------------------------------------------- | oAuth Config |-------------------------------------------------------------------------- */ /** * Storage */ 'storage' => new Session(), /** * Consumers */ 'consumers' => [ /** * Facebook */ 'Facebook' => [ 'client_id' => '', 'client_secret' => '', 'scope' => [], ], ] ];
凭证
将您的凭证添加到 config/oauth-5-laravel.php
(根据您选择的配置选项)
Storage 属性是可选的,默认为 Session。其他 选项。
使用
基本用法
只需按照以下步骤操作,您将能够获得一个 服务类对象,遵循以下规则
$fb = \OAuth::consumer('Facebook');
可选地,添加第二个参数,指定服务需要重定向到的 URL,否则将重定向到当前 URL。
$fb = \OAuth::consumer('Facebook', 'http://url.to.redirect.to');
用法示例
配置:将您的 Facebook 凭证添加到 config/oauth-5-laravel.php
'Facebook' => [ 'client_id' => 'Your Facebook client ID', 'client_secret' => 'Your Facebook Client Secret', 'scope' => ['email','read_friendlists','user_online_presence'], ],
在您的控制器中使用以下代码
public function loginWithFacebook(Request $request) { // get data from request $code = $request->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 ( ! is_null($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((string)$url); } }
配置:将您的 Google 凭证添加到 config/oauth-5-laravel.php
'Google' => [ 'client_id' => 'Your Google client ID', 'client_secret' => 'Your Google Client Secret', 'scope' => ['userinfo_email', 'userinfo_profile'], ],
在您的控制器中使用以下代码
public function loginWithGoogle(Request $request) { // get data from request $code = $request->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 ( ! is_null($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((string)$url); } }
配置:将您的 Twitter 凭证添加到 config/oauth-5-laravel.php
'Twitter' => [ 'client_id' => 'Your Twitter client ID', 'client_secret' => 'Your Twitter Client Secret', // No scope - oauth1 doesn't need scope ],
在您的控制器中使用以下代码
public function loginWithTwitter(Request $request) { // get data from request $token = $request->get('oauth_token'); $verify = $request->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 ( ! is_null($token) && ! is_null($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(['oauth_token' => $reqToken->getRequestToken()]); // return to twitter login url return redirect((string)$url); } }
配置:将您的 Linkedin 凭证添加到 config/oauth-5-laravel.php
'Linkedin' => [ 'client_id' => 'Your Linkedin API ID', 'client_secret' => 'Your Linkedin API Secret', ],
在您的控制器中使用以下代码
public function loginWithLinkedin(Request $request) { // get data from request $code = $request->get('code'); $linkedinService = \OAuth::consumer('Linkedin'); if ( ! is_null($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(['state'=>'DCEEFWF45453sdffef424']); // return to linkedin login url return redirect((string)$url); } }
###Yahoo
配置:将您的 Yahoo 凭证添加到 config/oauth-5-laravel.php
'Yahoo' => [ 'client_id' => 'Your Yahoo API KEY', 'client_secret' => 'Your Yahoo API Secret', ],
在您的控制器中使用以下代码
public function loginWithYahoo(Request $request) { // get data from request $token = $request->get('oauth_token'); $verify = $request->get('oauth_verifier'); \OAuth::setHttpClient('CurlClient'); // get yahoo service $yh = \OAuth::consumer('Yahoo'); // if code is provided get user data and sign in if ( ! is_null($token) && ! is_null($verify)) { // This was a callback request from yahoo, get the token $token = $yh->requestAccessToken($token, $verify); $xid = [$token->getExtraParams()]; $result = json_decode($yh->request('https://social.yahooapis.com/v1/user/' . $xid[0]['xoauth_yahoo_guid'] . '/profile?format=json'), true); //Var_dump //display whole array. dd($result); } // if not ask for permission first else { // get request token $reqToken = $yh->requestRequestToken(); // get Authorization Uri sending the request token $url = $yh->getAuthorizationUri(['oauth_token' => $reqToken->getRequestToken()]); // return to yahoo login url return redirect((string)$url); } }
更多用法示例
有关示例,请访问 这里