[精讚] [會員登入]
1243

使用Yahoo OAuth2 2/2

使用Yahoo OAuth2認證我的網頁

分享此文連結 //n.sfs.tw/10725

分享連結 使用Yahoo OAuth2 2/2 @新精讚
(文章歡迎轉載,務必尊重版權註明連結來源)
2019-10-21 16:48:45 最後編修
2017-02-05 01:57:00 By 張○○
 

此文分為兩個單元
使用Yahoo OAuth2 1/2
使用Yahoo OAuth2 2/2

前文已申請完yahoo的api後,接下來實作的部分,原先我參考了Jex’s note 不寫下來絕對會忘…的內容,原先我也如他的原先以為只要照著做,就能順利的運作,所以我就照著他的範例來做,但是怎麼試都會出現ERROR:

$session = YahooSession::requireSession(OAUTH::CONSUMER_KEY, OAUTH::CONSUMER_SECRET, OAUTH::APP_ID);

這裡面的YahooSession到底是什麼東西啊,為什麼突然出現?後來發現它引入https://github.com/yahoo/yos-social-php/blob/master/lib/OAuth.php這個library,它的年代已經過去,同時它的方法是用YQL的方式取回資料,所以我就和Jex說的一樣:我只好參考網路的資源重刻了一個輪子:

使用OAuth2的方法來作認證

雖然Jex無法解決我的問題,但摸索中也了解了oauth2的運作,這比直接得到一個輪子有意思。以下和Jex一樣,使用CI3作為範例:

四、建立一個library

這個library 來自於[3] 由saurabhsahni寫的library,/application/libraries/Yahoooauth2.php

<?php
/*
Example class to access Yahoo OAuth2 protected APIs, based on https://developer.yahoo.com/oauth2/guide/
Find documentation and support on Yahoo Developer Network: https://developer.yahoo.com/forums
*/
class YahooOAuth2 {
  const AUTHORIZATION_ENDPOINT  = 'https://api.login.yahoo.com/oauth2/request_auth';
  const TOKEN_ENDPOINT   = 'https://api.login.yahoo.com/oauth2/get_token';
  public function fetch($url,$postdata="",$auth="",$headers="") {
        $curl = curl_init($url);
        if($postdata) {
            curl_setopt($curl, CURLOPT_POST, true);
            curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($postdata));
        }
        else {
            curl_setopt($curl, CURLOPT_POST, false);
        }
        if($auth){
            curl_setopt($curl, CURLOPT_USERPWD, $auth);
        }
        if($headers){
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        }
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec( $curl );
        if (empty($response)) {
            // some kind of an error happened
            die(curl_error($curl));
            curl_close($curl); // close cURL handler
        } else {
            $info = curl_getinfo($curl);
            curl_close($curl); // close cURL handler
            if($info['http_code'] != 200 && $info['http_code'] != 201 ) {
                echo "Received error: " . $info['http_code']. "\n";
                echo "Raw response:".$response."\n";
                die();
            }
        }
        return $response;
  }
  public function getAuthorizationURL($client_id,$redirect_uri,$language="en-us") {
        $url = self::AUTHORIZATION_ENDPOINT;
        $authorization_url=$url.'?'.'client_id='.$client_id.'&redirect_uri='.$redirect_uri.
        '&language='.$language.'&response_type=code';
        return $authorization_url;
  }
  public function get_access_token($clientId, $clientSecret,$redirect_uri,$code) {
        $url=self::TOKEN_ENDPOINT;
        $postdata=array("redirect_uri"=>$redirect_uri,"code"=>$code,"grant_type"=>"authorization_code");
        $auth=$clientId . ":" . $clientSecret;
        $response=self::fetch($url,$postdata,$auth);
        // Convert the result from JSON format to a PHP array
        $jsonResponse = json_decode( $response );
        return $jsonResponse->access_token;
  }
}
?>

五、controller的部分,放在你的 controller function中

        $this->load->library('Yahoooauth2');
        define("CONSUMER_KEY", $this->config->item("yahoo_consumer_key"));
        define("CONSUMER_SECRET",$this->config->item("yahoo_consumer_secret"));
        $redirect_uri="http://".$_SERVER['SERVER_NAME'] . "/auth/login?step=8";  //設定你的回呼路徑
        $social_endpoint = "https://social.yahooapis.com/v1/user/me/profile?format=json";
        if (isset($_GET['code'])){
          $code=$_GET['code'];
        }
        else {
          $code=0;
        }
        if($code){
          try{
            #oAuth 3-legged authorization is successful, fetch access token
            $token=$this->yahoooauth2->get_access_token(CONSUMER_KEY,CONSUMER_SECRET,$redirect_uri,$code);
            #access token is available. Do API calls.
            $headers= array(
                'Authorization: Bearer '.$token,
                'Accept: application/json',
                'Content-Type: application/json'
            );
            //Fetch Advertiser Name and Advertiser ID
            $resp=$this->yahoooauth2->fetch($social_endpoint,$postdata="",$auth="",$headers);
            $jsonResponse = json_decode( $resp);
            $email = $jsonResponse->profile->emails[0]->handle;
            $name = $jsonResponse->profile->familyName .  $jsonResponse->profile->givenName;
            $gender =  ($jsonResponse->profile->gender=="M")? 1:0;
            $thumb = $jsonResponse->profile->image->imageUrl;
            $guid =  $jsonResponse->profile->guid;
         }catch(Exception $e){
              $msg= "Yahoo認證失敗:".$e->getMessage();
         }finally{
            print $msg;
         }
        }
        else {
        /* no valid access token available, go to authorization server */
          header("HTTP/1.1 302 Found");
          header("Location: " . $this->yahoooauth2->getAuthorizationURL(CONSUMER_KEY,$redirect_uri));
          exit;
        }

簡略說明:
2,3 行我把key/secret的值放在config中
4 設定你的回呼路徑
6-11,36-41 如果yahoo沒有回傳code,則導到yahoo進行授權確認
24 取得回呼的資料,是一個物件
25-29 回呼內容,可參考下面的物件內容:

六、取得的物件資料

stdClass Object

(

    [profile] => stdClass Object

        (

            [guid] => FRxxxxxxxxxxxxUU4

            [addresses] => Array

                (

                    [0] => stdClass Object

                        (

                            [city] =>

                            [country] => TW

                            [current] => 1

                            [id] => 1

                            [postalCode] =>

                            [state] =>

                            [street] =>

                            [type] => HOME

                        )

 

                    [1] => stdClass Object

                        (

                            [city] =>

                            [country] => TW

                            [current] => 1

                            [id] => 2

                            [postalCode] =>

                            [state] =>

                            [street] =>

                            [type] => WORK

                        )

 

                )

 

            [ageCategory] => A

            [created] => 2016-04-03T19:07:37Z

            [emails] => Array

                (

                    [0] => stdClass Object

                        (

                            [handle] => yourname@yahoo.com.tw

                            [id] => 1

                            [primary] => 1

                            [type] => HOME

                        )

 

                )

 

            [familyName] => 張

            [gender] => M

            [givenName] => .00

            [image] => stdClass Object

                (

                    [height] => 192

                    [imageUrl] => https://s.yimg.com/dh/ap/social/profile/profile_b192.png

                    [size] => 192x192

                    [width] => 192

                )

 

            [intl] => tw

            [jurisdiction] => tw

            [lang] => zh-Hant-TW

            [memberSince] => 2016-04-02T23:14:00Z

            [migrationSource] => 1

            [nickname] => .河馬

            [notStored] => 1

            [nux] => 0

            [phones] => Array

                (

                    [0] => stdClass Object

                        (

                            [id] => 10

                            [number] => 886-9xxxxxx1

                            [type] => MOBILE

                        )

 

                )

 

            [profileMode] => PUBLIC

            [profileStatus] => ACTIVE

            [profileUrl] => http://profile.yahoo.com/FRxxxxxxxxxU4

            [timeZone] => Asia/Shanghai

            [isConnected] => 1

            [profileHidden] =>

            [bdRestricted] => 1

            [profilePermission] => PRIVATE

            [uri] => https://social.yahooapis.com/v1/user/FRxxxxxxxxxxU4/profile

        )

)

 

能夠取得這麼完整的資料,相信你一定會很高興,祝你們使用愉快

參考資料

[3] https://github.com/saurabhsahni/php-yahoo-oauth2


原文 2016-04-05 00:04:26

END

你可能感興趣的文章

[CodeIgniter3] 解決無法上傳特定檔案(.sb2)的問題 上傳時出現The filetype you are attempting to upload is not allowed,要怎麼解決?

[CodeIgniter 3] 修改或插入資料時遇到函數的處理 CI3 中要新增或修改的資料中如果有 now()這類的函數,要怎麼處理?

[PHP] 判斷文字、數字、文字加數字的方法 幾個PHP數字和文字操作上的小眉角:判斷文字、數字、文字加數字的方法

[PHP+ci4] codeignitor4+ smarty5 這篇整合 php 的framework codeignitor4 + smarty5。

[PHP] IPv6檢查IP是否在某個網段內 mtachcidr6 要檢查IPv6是否在某個IPv6的網段內?

[PHP] 類別中要怎麼使用callback function 召回函數(回呼函數)? 在類別中使用標準函數,若其中的callback 函數也是在類別中,該怎麼使用?

我有話要說

>>

限制:留言最高字數1000字。 限制:未登入訪客,每則留言間隔需超過10分鐘,每日最多5則留言。

訪客留言

[無留言]

隨機好文

[jQuery] select 元件的取值及給值 html中的元件select,在jquery中要如何使用?

[Freebsd] 定時測試 ADSL 是否斷線並重連 中華電信 ADSL 雖有固定 ip,可是他卻會不定時「斷線」, 使用以下的 方法可以定時測試是否斷線,以及重新撥接。

PHP for sphinx 函式庫安裝 PECL/sphinx PHP>= 5.2.2 已經能原生支援 sphinx,可是預設的沒有裝,我們得自己裝才能用

[札記] 2016.7~12月札記 札記,只是札記

UTF-8的網頁但IE8一片空白 UTF8編碼的網頁在Firefox 正常顯示、但IE8 就是空白,IE8編碼設定是「自動偵測」可是自動偵測到的是 big5...