【问题标题】:Google API - Get contact list using PHPGoogle API - 使用 PHP 获取联系人列表
【发布时间】:2015-01-31 16:17:13
【问题描述】:

我想使用 PHP 在我的网站中获取 GMail 联系人列表。

还有我提到的教程Here

代码:

 <?php

// disable warnings
if (version_compare(phpversion(), "5.3.0", ">=")  == 1)
 error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
 else
 error_reporting(E_ALL & ~E_NOTICE); 

 $sClientId = '564766218700- 
 fgtj5fba9h15g8na4khdho1uavo0rtjq.apps.googleusercontent.com';
 $sClientSecret = 'GldOKp2S2ABdp-7owp3ZO_cE';
 $sCallback = 'http://localhost/GmailContact/index.php'; // callback url, don't forget 
 to change it to your!
 $iMaxResults = 20; // max results
 $sStep = 'auth'; // current step

 // include GmailOath library  https://code.google.com/p/rspsms/source/browse/trunk   
 /system/plugins/GmailContacts/GmailOath.php?r=11
 include_once('GmailOath.php');

 session_start();

 // prepare new instances of GmailOath  and GmailGetContacts
 $oAuth = new GmailOath($sClientId, $sClientSecret, $argarray, false, $sCallback);
 $oGetContacts = new GmailGetContacts();

 if ($_GET && $_GET['oauth_token']) {

$sStep = 'fetch_contacts'; // fetch contacts step

// decode request token and secret
$sDecodedToken = $oAuth->rfc3986_decode($_GET['oauth_token']);
$sDecodedTokenSecret = $oAuth->rfc3986_decode($_SESSION['oauth_token_secret']);

// get 'oauth_verifier'
$oAuthVerifier = $oAuth->rfc3986_decode($_GET['oauth_verifier']);

// prepare access token, decode it, and obtain contact list
$oAccessToken = $oGetContacts->get_access_token($oAuth, $sDecodedToken,   
$sDecodedTokenSecret, $oAuthVerifier, false, true, true);
$sAccessToken = $oAuth->rfc3986_decode($oAccessToken['oauth_token']);
$sAccessTokenSecret = $oAuth->rfc3986_decode($oAccessToken['oauth_token_secret']);
$aContacts = $oGetContacts->GetContacts($oAuth, $sAccessToken, $sAccessTokenSecret, 
false, true, $iMaxResults);

// turn array with contacts into html string
$sContacts = $sContactName = '';
foreach($aContacts as $k => $aInfo) {
    $sContactName = end($aInfo['title']);
    $aLast = end($aContacts[$k]);
    foreach($aLast as $aEmail) {
        $sContacts .= '<p>' . $sContactName . '(' . $aEmail['address'] . ')</p>';
    }
 }
 } else {
// prepare access token and set it into session
$oRequestToken = $oGetContacts->get_request_token($oAuth, false, true, true);
$_SESSION['oauth_token'] = $oRequestToken['oauth_token'];
$_SESSION['oauth_token_secret'] = $oRequestToken['oauth_token_secret'];
}

 ?>
<!DOCTYPE html>
<html lang="en" >
<head>
    <meta charset="utf-8" />
    <title>Google API - Get contact list | Script Tutorials</title>
    <link href="css/main.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <header>
        <h2>Google API - Get contact list</h2>
        <a href="http://www.script-tutorials.com/google-api-get-contact-list/" 
 class="stuts">Back to original tutorial on <span>Script Tutorials</span></a>
    </header>
    <img src="oauthLogo.png" class="google" alt="google" />

<?php if ($sStep == 'auth'): ?>
    <center>
    <h1>Step 1. OAuth</h1>
    <h2>Please click <a href="https://www.google.com/accounts
/OAuthAuthorizeToken?oauth_token=<?php echo 
$oAuth->rfc3986_decode($oRequestToken['oauth_token']) ?>">this link</a> in order to 
get    access token to receive contacts</h2>
    </center>
<?php elseif ($sStep == 'fetch_contacts'): ?>
    <center>
    <h1>Step 2. Results</h1>
    <br />
    <?= $sContacts ?>
    </center>
<?php endif ?>

我遇到如下错误:

缺少 OAuth 令牌参数。 这就是我们所知道的一切。

问题: 1. 如何获取 OAuth 访问令牌?

请帮帮我。

【问题讨论】:

  • 查看此链接以获取 oauth PHP 客户端 25labs.com/…
  • @SGC 我使用了上面指定的教程。但是我的电子邮件地址显示为空白。
  • @SGC echo($xmlrespose) 返回:usageLimits accessNotConfigured 访问未配置。您的项目未启用 API,或者您的 API 密钥上配置了 per-IP 或 per-Referer 限制,并且请求与这些限制不匹配。请使用 Google Developers Console 更新您的配置。 console.developers.google.com.
  • 您需要在您的谷歌控制台中为您的项目启用联系人 API。您是否可以选择使用付费进口商,例如CloudSponge
  • @RaelGugelminCunha 很好!!Ty

标签: php google-apps-script oauth-2.0 google-contacts-api


【解决方案1】:

有几种方法可以发出访问令牌请求,它们会根据您正在构建的应用程序的类型而有所不同。

例如,JavaScript 应用程序可能使用浏览器重定向到 Google 请求访问令牌,而安装在没有浏览器的设备上的应用程序使用 Web 服务请求。

某些请求需要用户使用其 Google 帐户登录的身份验证步骤。登录后,系统会询问用户是否愿意授予您的应用程序请求的权限。这个过程称为用户同意。

如果用户授予权限,Google 授权服务器会向您的应用程序发送访问令牌(或您的应用程序可用于获取访问令牌的授权代码)。如果用户不授予权限,则服务器返回错误。

这里是 link 的 oauth play ground,它有助于理解 Oauth 的概念。

另外,请查看此link 以获取联系人 API。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-09
    • 1970-01-01
    • 1970-01-01
    • 2016-06-05
    • 1970-01-01
    相关资源
    最近更新 更多