Google(OAuth2.0)PHP 授權登入
OAuth協議
OAuth(開放授權)是一個開放標準。
允許第三方網站在使用者授權的前提下訪問在使用者在服務商那裡儲存的各種資訊。
而這種授權無需將使用者提供使用者名稱和密碼提供給該第三方網站。
OAuth允許使用者提供一個令牌給第三方網站,一個令牌對應一個特定的第三方網站,同時該令牌只能在特定的時間內訪問特定的資源。
OAuth的原理和授權流程
OAuth的認證和授權的過程中涉及的三方包括:
服務商:使用者使用服務的提供方,一般用來存訊息、儲照片、視訊、聯絡人、檔案等(比如Twitter、Sina微波等)。
用 戶:服務商的使用者
第三方:通常是網站,該網站想要訪問使用者儲存在服務商那裡的資訊。
比如某個提供照片列印服務的網站,使用者想在那裡列印自己存在服務商那裡的網路相簿。
在認證過程之前,第三方需要先向服務商申請第三方服務的唯一標識。
OAuth認證和授權的過程如下:
1、使用者訪問第三方網站網站,想對使用者存放在服務商的某些資源進行操作。
2、第三方網站向服務商請求一個臨時令牌。
3、服務商驗證第三方網站的身份後,授予一個臨時令牌。
4、第三方網站獲得臨時令牌後,將使用者導向至服務商的授權頁面請求使用者授權,然後這個過程中將臨時令牌和第三方網站的返回地址傳送給服務商。
5、使用者在服務商的授權頁面上輸入自己的使用者名稱和密碼,授權第三方網站訪問所相應的資源。
6、授權成功後,服務商將使用者導向第三方網站的返回地址。
7、第三方網站根據臨時令牌從服務商那裡獲取訪問令牌。
8、服務商根據令牌和使用者的授權情況授予第三方網站訪問令牌。
9、第三方網站使用獲取到的訪問令牌訪問存放在服務商的對應的使用者資源。
一、建立新的專案,獲取 ClientID 和 Secret
1.在谷歌平臺申請自己的賬號,建立新的專案
2.新增網頁應用資訊
3.建立憑據
4.建立網頁應用,填寫回調地址
5.應用建立成功
6.檢視客戶端 ID和客戶端金鑰
二、部署程式碼
<?php
header('Content-Type:text/html; charset=utf-8');
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/11/14
* Time: 16:15
*/
class Google
{
protected $setting = [
'app_id' => 'skdjfkdj56.apps.googleusercontent.com', //客戶端ID
'redirect_uri' => 'http://www.ybaog.com/back.php', //回撥地址
'app_secret' => 'sudifjfiflkasifjafma658', //客戶端金鑰
];
public function __construct()
{
$this->client_id = $this- >setting['app_id'];
$this->client_secret = $this->setting['app_secret'];
$this->redirect_uri = $this->setting['redirect_uri'];
}
function index(){
//第一步:請求CODE
if (empty($_GET['code'])) {
$this->getCode($this->client_id,$this->redirect_uri);
} else {
//使用者允許授權後,將會重定向到redirect_uri的網址上,並且帶上code引數
$code=$_GET['code'];
$postData=array(
'code'=>$code,
'client_id'=>$this->client_id,
'client_secret'=>$this->client_secret,
'redirect_uri'=>$this->redirect_uri,
'grant_type'=>'authorization_code'
);
//第二步:通過code獲取access_token
$access_token=$this->getToken($postData);
if(empty($token)){
die( "<center><h2 style='color: red'>獲取TOKEN失敗!</h2></center>");
}
//第三步:通過access_token呼叫介面,獲取使用者資訊
$user=$this->getUserInfo($access_token);
//處理獲取到的的使用者資料。。。。。。
}
}
/**
* 抓取CODE
* @param $client_id
* @param $redirect_uris
*/
protected function getCode($client_id,$redirect_uris){
$redirect_uris=urlencode($redirect_uris);
$scope=urlencode('https://www.googleapis.com/auth/userinfo.profile');
$url = "https://accounts.google.com/o/oauth2/auth?response_type=code&access_type=offline&client_id={$client_id}&redirect_uri={$redirect_uris}&state&scope={$scope}&approval_prompt=auto";
header('Location:' . $url);
}
/**
* 抓取TOKEN
* @param $postData
* @param string $purl
* @return bool
*/
protected function getToken($postData,$purl='https://accounts.google.com/o/oauth2/token'){
$fields = (is_array($postData)) ? http_build_query($postData) : $postData;
$curlHeaders = [
'content-type: application/x-www-form-urlencoded;CHARSET=utf-8',
'Content-Length: ' . strlen($fields),
];
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $purl);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, $curlHeaders);
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
$responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if($response && $responseCode == 200){
$json = json_decode($response, true);
$token=$json['access_token'];
return $token;
}else {
return false;
}
}
/**
* 獲取使用者資訊
* @param $access_token
* @return array
*/
protected function getUserInfo($access_token){
$url = "https://www.googleapis.com/oauth2/v1/userinfo?access_token=".$access_token;
$userInfo = json_decode(file_get_contents($url),true);
$data['id']=$userInfo['id']; //ID
$data['name']=$userInfo['name']; //使用者名稱
$data['locale']=$userInfo['locale']; //語言
$data['picture']=$userInfo['picture']; //頭像
$data['given_name']=$userInfo['given_name']; //名字
$data['family_name']=$userInfo['family_name']; //姓
return $data;
}
}
$google=new Google;
$result=$google->index();
希望可以幫到你!!!