首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当代码在Google App Engine Server中时,未使用Oauth2从Gmail联系人API中获取访问令牌

当代码在Google App Engine Server中时,未使用Oauth2从Gmail联系人API中获取访问令牌
EN

Stack Overflow用户
提问于 2015-06-17 21:51:54
回答 2查看 510关注 0票数 0

我正在使用zend-framework2和google-app-engine服务器导入所有gmail联系人列表。我在登录后从gmail得到了授权码。当使用此授权码访问curl以获取访问令牌时,它会给出空白数组作为响应。

此外,这个应用程序是一个收费的应用程序在谷歌应用程序引擎。并且已经设置好了

代码语言:javascript
复制
extension = "curl.so"

在php.ini文件中。而且我提供的证书是正确的。

代码语言:javascript
复制
// Import Gmail Contacts
$client_id='xxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com'; 
$client_secret='xxxxxxxxxxxxxx-xxxxxxxxx';
$redirect_uri='http://xxxxxxxxxxxxxxx.appspot.com/u/invite/gmailCallback';
$max_results = 1000;
if(isset($_GET["code"]) && $_GET["code"] != '') {
    $auth_code = $_GET["code"];

    $fields=array(
        'code'=>  urlencode($auth_code),
        'client_id'=>  urlencode($client_id),
        'client_secret'=>  urlencode($client_secret),
        'redirect_uri'=>  urlencode($redirect_uri),
        'grant_type'=>  urlencode('authorization_code')
    );
    $post = '';
    foreach($fields as $key=>$value) { $post .= $key.'='.$value.'&'; }
    $post = rtrim($post,'&');

    $curl = curl_init();
    curl_setopt($curl,CURLOPT_URL,'https://accounts.google.com/o/oauth2/token');
    curl_setopt($curl,CURLOPT_POST,5);
    curl_setopt($curl,CURLOPT_POSTFIELDS,$post);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER,TRUE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,FALSE);
    $result = curl_exec($curl);
    //$info = curl_getinfo($curl);
    //echo "<pre>"; print_r($info); die;
    curl_close($curl);

    $response =  json_decode($result);
    //echo "<pre>"; var_dump($response); die;
    if(isset($response->access_token) && $response->access_token != '') {

        $accesstoken = $response->access_token;

        $url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results='.$max_results.'&oauth_token='.$accesstoken;
        //$url = 'https://www.google.com/m8/feeds/contacts/default/full?oauth_token='.$accesstoken;
        $xmlresponse =  $this->curl_file_get_contents($url);

        //At times you get Authorization error from Google.
        if((strlen(stristr($xmlresponse,'Authorization required'))>0) && (strlen(stristr($xmlresponse,'Error '))>0)) {
            //$returnArr['error_msg'] = "Something went wrong to import contacts!!";
            echo '<h2>Something went wrong to import contacts !!</h2>';die;
        } else {
            $xml =  new SimpleXMLElement($xmlresponse);
            $xml->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005');
            $result = $xml->xpath('//gd:email');
            //echo "<pre>"; print_r($result); die;
            foreach ($result as $title) {
                $emailsArr[] = (string) $title->attributes()->address;
            }
        }
    }

}

请帮帮忙。

EN

回答 2

Stack Overflow用户

发布于 2015-06-17 23:47:58

根据以前的经验(和this的回答),我不认为App Engine上启用了cURL,因为它不支持C扩展。

你得用URL Fetch代替,例如:

代码语言:javascript
复制
$data = ['data' => 'this', 'data2' => 'that'];
$data = http_build_query($data);
$context = [
  'http' => [
    'method' => 'GET',
    'header' => "custom-header: custom-value\r\n" .
                "custom-header-two: custom-value-2\r\n",
    'content' => $data
  ]
];

$context = stream_context_create($context);
$result  = file_get_contents('http://app.com/path?query=update', false, $context);
票数 0
EN

Stack Overflow用户

发布于 2015-08-18 18:44:08

App engine支持cURL,但似乎有缺陷。我在通过谷歌进行OAuth身份验证时遇到了同样的问题。POST cURL调用只返回FALSE,没有任何错误消息。这是官方的错误报告:https://code.google.com/p/googleappengine/issues/detail?id=11985

你现在能做的就是切换到url抓取功能。要发出POST请求,可以使用以下函数:

代码语言:javascript
复制
protected function _call($url, array $params, $method = 'GET', array $headers = [])
{        
    $query = http_build_query($params);

    $headers_str = '';

    if ($headers) {
        foreach ($headers as $name => $value) {
            $headers_str .= $name . ': ' . $value . "\r\n";
        }
    }

    $headers_str .= "Content-type: "."application/x-www-form-urlencoded"."\r\n";

    $options =
        array('http'=>
          array(
            'method' => $method,
            'header' => $headers_str,
            'content' => $query,
            'ignore_errors' => true,
          )
    );

    if ($method === 'POST') {
        $options['http']['content'] = $query;
    } else if ($query) {
        $url .= '?' . $query;
    }

    $context = stream_context_create($options);

    $response = file_get_contents($url, false, $context);

    // check for zipped response and decode it
    if (array_search('Content-Encoding: gzip', $http_response_header)) {
        $response = gzinflate(substr($response, 10, -8));
    }

    return $response;
}

或者,您可以尝试使用这个cURL包装库https://github.com/azayarni/purl,它是在GAE提供cURL支持之前实现的。希望对您有所帮助:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30893648

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档