<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ## 一、第三方登錄的原理 所謂第三方登錄,實質就是 OAuth 授權。用戶想要登錄 A 網站,A 網站讓用戶提供第三方網站的數據,證明自己的身份。獲取第三方網站的身份數據,就需要 OAuth 授權。 舉例來說,A 網站允許 GitHub 登錄,背后就是下面的流程。 > 1. A 網站讓用戶跳轉到 GitHub。 > 2. GitHub 要求用戶登錄,然后詢問"A 網站要求獲得 xx 權限,你是否同意?" > 3. 用戶同意,GitHub 就會重定向回 A 網站,同時發回一個授權碼。 > 4. A 網站使用授權碼,向 GitHub 請求令牌。 > 5. GitHub 返回令牌. > 6. A 網站使用令牌,向 GitHub 請求用戶數據。 ## 二、應用登記 一個應用要求 OAuth 授權,必須先到對方網站登記,讓對方知道是誰在請求。 所以,你要先去 GitHub 登記一下。當然,我已經登記過了,你使用我的登記信息也可以,但為了完整走一遍流程,還是建議大家自己登記。這是免費的。 訪問這個[網址](https://github.com/settings/applications/new),填寫登記表。 ![](https://img.kancloud.cn/8e/b1/8eb16caae5456af933edb8854485a551_517x436.png) ![](https://img.kancloud.cn/5d/b4/5db4d5d3dd755760e646b967f5ca3df0_493x412.png) [OAuth文檔](https://developer.github.com/v3/oauth/) 應用的名稱隨便填,主頁 URL 填寫`http://localhost:80`,跳轉網址填寫`http://localhost:80/oauth/redirect`。 提交表單以后,GitHub 應該會返回客戶端 ID(client ID)和客戶端密鑰(client secret),這就是應用的身份識別碼。 ![](https://img.kancloud.cn/9a/6c/9a6c311b2557ad8e3f320bed190fbd8e_413x107.png) ***** ``` <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>OAuth2 Demo</title> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <a id="login">GitHub登錄</a> <script> // fill in your cliend_id const client_id = 'cdeca9e8ee6c9522d22e'; const authorize_uri = 'https://github.com/login/oauth/authorize'; //這個地址是瀏覽器跳轉的,獲取code成功后guthub會將這個跳轉地址與code一起回應給客戶端,并且httpcode是302(告訴瀏覽器跳轉到這個地址,瀏覽器知道是302后就會自動跳轉了) const redirect_uri = 'http://localhost:80/oauth/redirect.php'; const link = document.getElementById('login'); link.href = `${authorize_uri}?client_id=${client_id}&redirect_uri=${redirect_uri}`; </script> </body> </html> ``` 點擊GitHub登錄連接跳轉至github,登錄github后跳至授權頁面 ![](https://img.kancloud.cn/fb/31/fb316ef954fd9ca1984823be3f31e5f0_470x357.png) 跳轉格式如下: ``` https://github.com/login/oauth/authorize?client_id=xxoo&redirect_uri=xxoo ``` 這個 URL 指向 GitHub 的 OAuth 授權網址,帶有兩個參數:`client_id`告訴 GitHub 誰在請求,`redirect_uri`是稍后跳轉回來的網址 確認后,github攜帶code跳轉至http://localhost/oauth/redirect.php?code=2c2795dda93994a15c8c 七、令牌 后端使用這個授權碼,向 GitHub 請求令牌。 redirect.php ``` /** * url get或者post方式請求 * * @param string $url 要請求的url地址 * @param array $data 要發送的數據 存在該參數就啟用post提交,否則為get請求 * @return mixed $output 返回請求的結果 */ function curl($url, $data = null){ // 1.創建一個新的CURL資源 $ch = curl_init(); // 2.設置URL相應的選項 curl_setopt($ch, CURLOPT_URL, $url); // 設置請求的URL地址 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 將curl_exec()獲取的信息以文件流的形式返回,而不是直接輸出(0為直接輸出) //curl_setopt($ch, CURLOPT_HEADER, 0); // 啟用時會將頭文件的信息作為數據流輸出 curl_setopt($ch, CURLOPT_TIMEOUT, 10); // 設置cURL允許執行的最長秒數 curl_setopt($ch, CURLOPT_POST, $data ? 1 : 0); // 存在data就啟用post發送 啟用時會發送一個常規的POST請求,類型為:application/x-www-form-urlencoded,就像表單提交的一樣。 curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //data必須數組 //忽略證書 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); if (!empty($header)) { curl_setopt($ch,CURLOPT_HTTPHEADER,$header); }else{ curl_setopt($curl, CURLOPT_HEADER, 0);//返回response頭部信息 } // 3.抓去URL并將它傳遞給瀏覽器 $output = curl_exec($ch); // 4.關閉CURL資源,并且釋放系統資源 curl_close($ch); // 返回輸出 return $output; } $data=[ 'client_id'=>'cdeca9e8ee6c9522d22e', 'client_secret'=>'e6e767515f87aa45c9a5027f6982f6abf90d0d2b', 'code'=>$_GET['code'], ]; $res=curl('https://github.com/login/oauth/access_token',$data); var_dump($res); //請求 $header=[ 'accept'=> 'application/json', //'User-Agent'=> 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36', 'User-Agent'=> 'lichihua', 'Authorization'=> "bearer {$access_token}" ]; //get請求 $res=curl('https://api.github.com',null,$header); var_dump($res); ``` 返回結果: ``` string(78) "access_token=fe25c09162fc631ccb8b02b03654dd649026c1d2&scope=&token_type=bearer" ``` 上面代碼中,GitHub 的令牌接口`https://github.com/login/oauth/access_token`需要提供三個參數。 > * `client_id`:客戶端的 ID > * `client_secret`:客戶端的密鑰 > * `code`:授權碼 作為回應,GitHub 會返回令牌`accessToken`。 node例子請看[http://www.ruanyifeng.com/blog/2019/04/github-oauth.html](http://www.ruanyifeng.com/blog/2019/04/github-oauth.html)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看