2015/04/20

Facebook And Google Login Upgrade

這次 Faceook 規定 4/30 要升級到比較新的套件,Google 則是 4/20 以後要禁止使用 openid 2.0 登入,列一下 PHP 端製作的筆記。

Facebook

首先你要下載 Facebook PHP 的 SDK,安裝方法有 composer 或直接下載,PHP 版本需求為 5.4+。

Composer
{
  "require" : {
    "facebook/php-sdk-v4" : "4.0.*"
  }
}
GitHub

https://github.com/facebookarchive/facebook-php-sdk

PHP Code
<?php

use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
use Facebook\FacebookRedirectLoginHelper;

$id = 'YOUR CLIENT ID';
$secret = 'YOUR SECRET';
$url = 'RETURN URL';

// Pemission scope, type is array
// reference url: https://developers.facebook.com/docs/facebook-login/permissions/v2.3
$permissions = 'YOUR PERMISSIONS SCOPE';

FacebookSession::setDefaultApplication($id, $secret);
$helper = new FacebookRedirectLoginHelper($url);

try {
    if(isset($_SESSION['access_token']) === true) {
        $session = new FacebookSession($_SESSION['access_token']);
    } else {
        $session = $helper->getSessionFromRedirect();
    }
} catch(FacebookRequestException $ex) {
    // When Facebook returns an error
    $ex->getMessage();
} catch(\Exception $ex) {
    // When validation fails or other local issues
    $ex->getMessage();
}

if ($session) {
    $_SESSION['access_token'] = $session->getToken();

    // Get all scope id
    $request = (new FacebookRequest($session, 'GET', '/me/ids_for_business'))->execute();
    $idGroup = (array)$request->getGraphObject()->getProperty('data')->asArray();

    // Get user info
    $request = (new FacebookRequest($session, 'GET', '/me'))->execute();
    $user = $request->getGraphObject()->asArray();
} else {
    // Echo login url
    echo $helper->getLoginUrl($permissions);
}

Google

安裝方法一樣有兩種,composer 跟 github,PHP 要求為 5.2+。

Composer
"require": {
  "google/apiclient": "1.0.*@beta"
}
GitHub

https://github.com/google/google-api-php-client

PHP Code
<?php

$id = 'YOUR CLIENT ID';
$secret = 'YOUR SECRET';
$url = 'RETURN URL';

$client = new Google_Client();
$client->setClientId($id);
$client->setClientSecret($secret);
$client->setRedirectUri($url);
$client->setScopes(
    array(
        'https://www.googleapis.com/auth/userinfo.email',
        'https://www.googleapis.com/auth/userinfo.profile',
    )
);
$service = new Google_Service_Oauth2($client);

if (isset($_GET['code']) === true) {
    $client->authenticate($_GET['code']);
    $_SESSION['access_token'] = $client->getAccessToken();

    header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}

if (isset($_SESSION['access_token']) === true) {
    $client->setAccessToken($_SESSION['access_token']);
}

if ($client->getAccessToken()) {
    // User info
    $me = $service->userinfo->get();
} else {
    $authUrl = $client->createAuthUrl();

    // Login url
    echo $authUrl;
}

沒有留言: