Google 联系 API 'failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden'

Google contact API 'failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden'

我在使用 OAuth 使用 PHP Codeigniter 连接到 Google 联系人 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 响应。请帮助..

此错误来自 file_get_contents 命令

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

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

Topic