【问题标题】:Google contact API 'failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden'Google 联系人 API '未能打开流:HTTP 请求失败! HTTP/1.0 403 禁止'
【发布时间】:2017-01-06 13:49:19
【问题描述】:

我在使用 OAuth 通过 PHP Codeigniter 连接到 Google Contacts API 时遇到问题。看来我可以连接,但接收

Message: file_get_contents(https://www.google.com/m8/feeds/contacts/default/full?&max-results=50&oauth_token=<token string>): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden

在最后的过程中。这是代码,

        // Google Project API Credentials
    $Google_api_client_id = 'client-id';
    $Google_client_secret = 'client-secret';
    $Google_redirect_url = base_url() . 'contact_import/';
    $Google_contact_max_result = 50; // integer value
    $authcode= $_GET['code'];
    $clientid=$Google_api_client_id;
    $clientsecret=$Google_client_secret;
    $redirecturi=$Google_redirect_url ;
    $fields=array(
    'code'=>  urlencode($authcode),
    'client_id'=>  urlencode($clientid),
    'client_secret'=>  urlencode($clientsecret),
    'redirect_uri'=>  urlencode($redirecturi),
    'grant_type'=>  urlencode('authorization_code')
    );
    $fields_string="";
    foreach($fields as $key=>$value) 
        { $fields_string .= $key.'='.$value.'&'; }
    $fields_string=rtrim($fields_string,'&');
    //open connection
    $ch = curl_init();
    //set the url, number of POST vars, POST data
    curl_setopt($ch,CURLOPT_URL,'https://accounts.google.com/o/oauth2/token');
    curl_setopt($ch,CURLOPT_POST,5);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
    // Set so curl_exec returns the result instead of outputting it.
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    //to trust any ssl certificates
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    //execute post
    $result = curl_exec($ch);
    //close connection
    curl_close($ch);
    //extracting access_token from response string
    $response   =  json_decode($result);
    $accesstoken= $response->access_token;
    if( $accesstoken!="")
        $_SESSION['token']= $accesstoken;
    //passing accesstoken to obtain contact details
    $xmlresponse=  file_get_contents('https://www.google.com/m8/feeds/contacts/default/full?&max-results='.$Google_contact_max_result.'&oauth_token='.$_SESSION['token']);
    //reading xml using SimpleXML
    $xml=  new SimpleXMLElement($xmlresponse);
    $xml->registerXPathNamespace('gd', 'http://schemas.google.com/g/2008');
    $result = $xml->xpath('//gd:email');
    $count = 0;
    foreach ( $result as $title )
    {
        $fetched_email = $title->attributes()->address;
        $contact_key[] = $this->db->insert_contact_gmail($fetched_email);
    }

如果我在浏览器中复制粘贴带有令牌的 URL,我可以看到 XML 响应。请帮忙。。

【问题讨论】:

  • 如果你粘贴你的 url 并且它有效,这是一个 GET 请求。在您的代码中,您正在执行POST 请求。不确定这是主要问题,但首先检查一下

标签: php codeigniter oauth google-api google-contacts-api


【解决方案1】:

这个错误来自file_get_contentscommand

您必须在php.ini 中设置allow-url-fopen,如果您在线工作,则必须向站点管理员询问

这个话题可能对你有帮助:

Topic

【讨论】:

  • 'allow_url_fopen' 已在 ini 文件中启用。无法解决问题。
  • 这不是这里的问题,那么错误将是别的东西
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-09
  • 1970-01-01
  • 2020-08-06
  • 1970-01-01
相关资源
最近更新 更多