谷歌联系人API php代码停止工作

时间:2018-01-23 03:51:37

标签: oauth-2.0 google-contacts

我使用以下代码获取gmail联系人,但它突然停止工作。相同的代码适用于另一个域。正在从文件中正确检索凭据。该域名已经过Google验证。

任何帮助都将不胜感激。

    //setup new google client
    $client = new Google_Client();
    $client -> setClientid($google_client_id);
    $client -> setClientSecret($google_client_secret);
    $client -> setRedirectUri($google_redirect_uri);
    $client -> setAccessType('online');
    $client -> setScopes('https://www.google.com/m8/feeds');

    $googleImportUrl = $client -> createAuthUrl();
    //echo 'google import url: '.$googleImportUrl.'<br>';

function curl($url, $post = "") {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
    if ($post != "") {
        curl_setopt($curl, CURLOPT_POST, 5);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
    }
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($curl, CURLOPT_AUTOREFERER, TRUE);
    curl_setopt($curl, CURLOPT_TIMEOUT, 10);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    $contents = curl_exec($curl);
    curl_close($curl);
    if($contents === FALSE) {
        die(curl_error($ch));
    }

    return $contents;
}

if (isset($_GET['code'])) {
    $auth_code = $_GET["code"];
    $_SESSION['google_code'] = $auth_code;
    header('Location: ' . $google_redirect_uri);
}

if(isset($_SESSION['google_code'])) {
    $auth_code = $_SESSION['google_code'];
    $max_results = 10000;
    $fields=array(
        'code'=>  urlencode($auth_code),
        'client_id'=>  urlencode($google_client_id),
        'client_secret'=>  urlencode($google_client_secret),
        'redirect_uri'=>  urlencode($google_redirect_uri),
        'grant_type'=>  urlencode('authorization_code')
    );
    $post = '';
    foreach($fields as $key=>$value)
    {
        $post .= $key.'='.$value.'&';
    }
    $post = rtrim($post,'&');
    $result = curl('https://accounts.google.com/o/oauth2/token',$post);
    $response =  json_decode($result);
    $accesstoken = $response->access_token;
    $url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results='.$max_results.'&alt=json&v=3.0&oauth_token='.$accesstoken;
    $xmlresponse =  curl($url);
    $contacts = json_decode($xmlresponse,true);
    unset($_SESSION['google_code']);
}

0 个答案:

没有答案