2017-01-06 2 views
1

OAuth를 사용하여 PHP Codeigniter가있는 Google Contacts API에 연결하는 데 문제가 있습니다. 내가 연결할 수 있으나, 최종 과정에서Google 담당자 API '스트림을 열지 못했습니다 : HTTP 요청에 실패했습니다! HTTP/1.0 403 Forbidden '

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 응답을 볼 수 있습니다. 제발 도와주세요 ..

+0

URL을 붙여넣고 작동하면 'GET'요청입니다. 그리고 코드에서'POST' 요청을합니다. 주된 문제인지는 모르겠지만 먼저 이것을 확인하십시오 – Iamzozo

답변

1

이 오류는 file_get_contents 명령에서 오는

당신은 php.ini 년에 allow-url-fopen을 설정해야하고 온라인으로 작업하는 경우가

이 항목이있을 수 있습니다 사이트 adminestor에서 당신을 도움을 요청해야합니다 :

Topic

+0

'allow_url_fopen'이 (가) ini 파일에서 이미 사용 설정되어 있습니다. 문제를 파악할 수 없습니다. – Sreejith

관련 문제