Google联系API无法打开流:HTTP请求失败! HTTP / 1.0 403 Forbidden'

时间:2017-01-06 13:49:20

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

我在使用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响应。请帮忙..

1 个答案:

答案 0 :(得分:1)

此错误来自allow-url-fopen命令

您必须在php.ini设置CREATE TABLE dbo.AgentAndOsFilter ( Id INT IDENTITY(1, 1), AgentVersion VARCHAR(8), Os VARCHAR(16) ) ,如果您在线工作,则必须从网站adminestor上提出

本主题可能会对您有所帮助:

Topic