簡體   English   中英

適用於Android的PHP Google OAuth2后端

[英]PHP Google OAuth2 Backend for Android

我按照本教程( http://blog.jachobsen.com/2013/08/10/google-oauth2-in-android-with-rails-backend/ )創建了oauth2身份驗證的android部分。 最后,它提供了服務器后端代碼的Rails部分,但是我對Rails不太滿意,因此也不太確定該怎么做。

有誰知道我可以用PHP創建類似的東西嗎? 我已經看過了這個https://code.google.com/p/google-api-php-client/,但是我無法編輯代碼以獲取訪問令牌,檢查是否有效以及然后返回一個API密鑰。

非常感謝,丹尼爾

您要查看的PHP示例不是最新的Google+ /身份驗證。 您應該從Google+文檔中的最新內容開始:

Google+ PHP快速入門

快速入門向您展示如何授權客戶端並將憑證傳遞給PHP后端以進行API調用。

如果您的服務器無法運行Phar,則可以從鏈接的PHP客戶端庫頁面開始使用示例代碼,並可以對其進行更新,以根據傳遞給您的Android應用的代碼進行代碼交換,或授權用戶訪問來自網絡的令牌/授權代碼。

以下示例執行Web的代碼交換(如quickstart示例中的/ connect端點中一樣):

<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_PlusService.php';

// Set your cached access token. Remember to replace $_SESSION with a
// real database or memcached.
session_start();

$client = new Google_Client();
$client->setApplicationName('Google+ PHP Starter Application');
// Visit https://code.google.com/apis/console?api=plus to generate your
// client id, client secret, and to register your redirect uri.
$client->setClientId('YOUR_CLIENT_ID');
$client->setClientSecret('YOUR_CLIENT_SECRET');
$client->setDeveloperKey('YOUR_SIMPLE_API_KEY');

$plus = new Google_PlusService($client);

if (isset($_GET['webcode'])) {
  $client->setRedirectUri('postmessage');
  $client->authenticate($_GET['webcode']);
  $_SESSION['token'] = $client->getAccessToken();

  $activities = $plus->activities->listActivities('me', 'public');
  print 'Your Activities: <pre>' . print_r($activities, true) . '</pre>';
}

要查看代碼的工作原理,您將需要從Web客戶端生成代碼:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
<span id="signinButton">
<span
      class="g-signin"
      data-callback="signinCallback"
      data-clientid="YOUR_CLIENT_ID"
      data-cookiepolicy="single_host_origin"
      data-requestvisibleactions="http://schemas.google.com/AddActivity"
      data-scope="https://www.googleapis.com/auth/plus.login">
    </span>
  </span>
  <table>
    <tr>
      <th>Code</th><th>ID Token</th><th>Access token</th>
    </tr>
    <tr>
      <td><textarea id="code"></textarea></td>
      <td><textarea id="idtok"></textarea></td>
      <td><textarea id="atok"></textarea></td>
    </tr>
  </table>

  <script type="text/javascript">
      (function () {
          var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
          po.src = 'https://apis.google.com/js/client:plusone.js';
          var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
      })();

      function signinCallback(resp) {
          console.log(resp);
          document.getElementById('code').value = resp.code;
          document.getElementById('idtok').value = resp.id_token;
          document.getElementById('atok').value = resp.access_token;
      }
  </script>
</body>
</html>

然后將其作為webcode參數傳遞給php腳本。 我創建了一個從wheresgus.com/phptest/stackdemo.php運行的演示,您可以從http://wheresgus.com/phptest/gencode.html生成代碼

GET示例如下所示:

http://wheresgus.com/phptest/stackdemo.php?webcode=4/ajfCXQiZo-zRBAJGktP_eSYRha2s.YiEFJjWUiW4bEnp6UAPFm0GQNJMGhgI

實際上,您應該通過HTTPS發布代碼; 但是,為了幫助您入門,希望對您有所幫助。

對於移動客戶端,您應該能夠使用ID令牌進行驗證,如下所示:

  • 安全地將ID令牌傳遞到您的應用程序
  • 驗證令牌是否有效並屬於正確的應用
  • 使用子字段標識您的用戶

以下代碼顯示了同一應用程序中的一個功能,該功能可驗證令牌並使用簡單的API訪問權限為登錄用戶執行activity.list:

if (isset($_GET['idtoken'])) {
  $attributes = $client->verifyIdToken($_GET['idtoken'], CLIENT_ID)
      ->getAttributes();
  $gplus_id = $attributes["payload"]["sub"];

  // At a minimum, make sure the token was for this app.
  if ($attributes["payload"]["aud"] == $client->getClientId()){
    $activities = $plus->activities->listActivities($gplus_id, 'public');
    print 'Your Activities: <pre>' . print_r($activities, true) . '</pre>';
  }else{
    print 'Authorization failed.';
  }
}

完整的代碼在這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM