如何使用谷歌API从Gmail获取联系人并在PHP中联系抓取程序?

时间:2014-10-12 14:08:30

标签: api contacts

在我的项目中,我想使用google API从gmail中获取联系人。我提供了三个密钥,例如客户端ID,客户端密钥和签名密钥。当我试图获取联系人时,弹出窗口显示错误消息“必须格式化签名密钥”。我做错了什么?提前谢谢。

这是我的代码
keyConfig.php

<?php 
$apiRequestUrl = "https://stescodes.com/api/contactsapi.aspx"; // StesCodes contact grabber API request url
$originalsignaturekey = "lxFWDA5ng36sdvlFGukof75vyi";//replace with your signature key

$gmailConsumerKey = "1009516162377-n5s7lo5b4dvlt8e7s3rt12f8i02lpk1f.apps.googleusercontent.com"; 
$gmailConsumerSecret = "raCUba1smsZCzrVNjqFIqiqC";

$YahooConsumerKey = "your yahoo api key";
$YahooConsumerSecret = "your yahoo api key";

$LiveConsumerKey = "your live api key";
$LiveConsumerSecret = "your live api key";

$fbConsumerKey = "your facebook api key";
$fbConsumerSecret = "your facebook api key";

$callbackurl = "http://localhost/grab/oauth.php";// eg: return url after user authentication http://yourdomain/oauth.php

$currentdirpath = "http://dev.stescodes.com/";//your current web directory path eg:http://yourdomain/
?>  

oauth.php

<?php
session_start();
?>
<?php include 'keyConfig.php'; ?>
<html>
<head><title></title>
<script>
function redirectrequest(a)
{
    window.location = a;
}
function closepopup(a,b,c,d) {
    window.opener.startGrabbingContactsOauth(a,b,c,d);
    window.self.close();
}
</script>
</head>
<body>
<?php
$servicename = "gmail"; 
$token = ""; 
$ConsumerKey = "";
$ConsumerSecret="";
$tokensecret="";
$tokenverifier="";
$flag=0;
$parameters="";
if($_GET['currpage']!=null)
    $_SESSION['currpage']=$_GET['currpage'];
if($_SESSION['currpage']=="gmail")
{
    $servicename = "gmail";
    $ConsumerKey = $gmailConsumerKey;
    $ConsumerSecret = $gmailConsumerSecret;
    if ($_GET['code'] != null)
    {
        $token = $_GET['code'];
        $tokensecret = $_SESSION['tokensecret'];
        $flag = 1;
    }
}
else if($_SESSION['currpage']=="yahoo")
{
    $servicename = "yahoo";
    $ConsumerKey = $YahooConsumerKey;
    $ConsumerSecret = $YahooConsumerSecret;
    if (($_GET['oauth_token'] != null) && ($_GET['oauth_verifier'] != null))
    {
        $token = $_GET['oauth_token'];
        $tokenverifier = $_GET['oauth_verifier'];
        $tokensecret = $_SESSION['tokensecret'];
        $flag = 1;
    }
}
else if($_SESSION['currpage']=="facebook")
{
    $servicename = "facebook";
    $ConsumerKey = $fbConsumerKey;
    $ConsumerSecret = $fbConsumerSecret;
    if (($_GET['code'] != null))
    {
        $token = $_GET['code'];
        $tokenverifier = "";
        $tokensecret = "";
        $flag = 1;
    }
}
else if(($_SESSION['currpage']=="msn") || ($_SESSION['currpage']=="hotmail") || ($_SESSION['currpage']=="msnmessenger"))
{
    $servicename = $_SESSION['currpage'];
    $ConsumerKey = $LiveConsumerKey;
    $ConsumerSecret = $LiveConsumerSecret;

    //Live settings
    if ($_GET["code"] != null)
    {
        $token = $_GET["code"];
        $flag = 1;
    }


}
if ($flag == 1)
{
    $parameters = "type=accesstoken&ServiceName=" . urlencode($servicename) . "&ConsumerKey=" . urlencode($ConsumerKey) . "&ConsumerSecret=" . urlencode($ConsumerSecret);
    $parameters = $parameters . "&ReturnUrl=" . urlencode($callbackurl) . "&Token=" . urlencode($token) . "&TokenSecret=" . urlencode($tokensecret) . "&TokenVerifier=" . urlencode($tokenverifier);
    $result  = file_get_contents($apiRequestUrl."?".$parameters);
    $authdetails = json_decode($result,true);
    $_SESSION['token'] = $authdetails[details][token];
    $_SESSION['tokensecret'] = $authdetails[details][tokenSecret];
    $_SESSION['uid'] = $authdetails[details][userID];
    $_SESSION['tokenverifier'] = $_SESSION['tokenverifier'];
    $_SESSION["consumerkey"] = $ConsumerKey;
    $_SESSION["consumersecret"] = $ConsumerSecret;
    echo "<SCRIPT LANGUAGE=\"javascript\"> closepopup('".$servicename."',". $result .",'".$ConsumerKey."','".$ConsumerSecret."');</SCRIPT>";
}
else
{     
    $parameters = "type=authenticationurl&ServiceName=" . urlencode($servicename) . "&ConsumerKey=" . urlencode($ConsumerKey) . "&ConsumerSecret=" . urlencode($ConsumerSecret);
    $parameters = $parameters . "&ReturnUrl=" . urlencode($callbackurl) ;
    $result  = file_get_contents($apiRequestUrl."?".$parameters);

    $authdetails = json_decode($result,true);

    $_SESSION['token'] = $authdetails[details][token];
    $_SESSION['tokensecret'] = $authdetails[details][tokenSecret];
    $_SESSION['uid'] = $authdetails[details][userID];
    $_SESSION['tokenverifier'] = $tokenverifier;
    echo "<SCRIPT LANGUAGE=\"javascript\"> redirectrequest('".$authdetails[details][authUrl]."');       </SCRIPT>";
}
?>

0 个答案:

没有答案