6

लौट रहा है मैं यहां मिले क्लाइंट लाइब्रेरी का उपयोग करके निम्न PHP कोड चला रहा हूं: https://code.google.com/p/google-api-php-client/। मुझे इस कोड में से किसी के लिए कोई त्रुटि नहीं है, लेकिन जब मैं getAccessToken() पर कॉल करता हूं, तो यह शून्य हो जाता है।सेवा खाते का उपयोग करके, getAccessToken() शून्य

मैंने अपने व्यक्तिगत कैलेंडर पर इस सेवा खाते तक पहुंच की अनुमति दी है, और एपीआई कंसोल के माध्यम से परियोजना तक पूर्ण पहुंच प्रदान की है।

कोई विचार?

require_once 'google-api-php-client/src/Google_Client.php'; 

const CLIENT_ID = 'blahblahblah'; 
const SERVICE_ACCOUNT_NAME = '[email protected]'; 
const KEY_FILE = 'path/to/privatekey.p12'; 

$google_client = new Google_Client(); // created only to initialized static dependencies 
$client = new Google_OAuth2(); // you really just need Google_OAuth2 

$key = file_get_contents(KEY_FILE); 

$client->setAssertionCredentials(
    new Google_AssertionCredentials(
     SERVICE_ACCOUNT_NAME, 
     array('https://www.googleapis.com/auth/calendar'), 
     $key 
    ) 
); 

var_dump($client->getAccessToken()); 

उत्तर

3

किसी कारण के लिए, यह काम करने के लिए लग रहा था:

require_once 'google-api-php-client/src/Google_Client.php'; 

const CLIENT_ID = 'blahblahblah'; 
const SERVICE_ACCOUNT_NAME = '[email protected]'; 
const KEY_FILE = 'path/to/privatekey.p12'; 
const CALENDAR_SCOPE = "https://www.googleapis.com/auth/calendar"; 

$key = file_get_contents(KEY_FILE); 
$auth = new Google_AssertionCredentials(
    SERVICE_ACCOUNT_NAME, 
    array(CALENDAR_SCOPE), 
    $key 
); 

$client = new Google_Client(); 
$client->setScopes(array(CALENDAR_SCOPE)); 
$client->setAssertionCredentials($auth); 
$client->getAuth()->refreshTokenWithAssertion(); 
$accessToken = $client->getAccessToken(); 

$client->setClientId(CLIENT_ID); 

किसी की व्याख्या कर सकते हैं क्यों इस काम किया है, तो कृपया इस जवाब को संपादित करने या टिप्पणी!

+0

या तो सुनिश्चित नहीं है। मैं https://gist.github.com/fulldecent/6728257 पर क्रेडिट के साथ अपने कोड का उपयोग कर रहा हूं और एफवाईआई लाइन सेटस्कोप आवश्यक नहीं है –

+1

'getAccessToken()' '' पहुँच पहुंचने वाला टोकन लौटाता है, जो तब तक मौजूद नहीं है जब तक आप कॉल नहीं करते 'refreshTokenWithAssertion()' '(इसे आरंभ करने के लिए) या' 'सेटएपटाउन()' '(यदि आपने पिछली एक्सेस टोकन पुनर्प्राप्त की है जिसे आप उपयोग करना चाहते हैं)। – tiho

संबंधित मुद्दे