यहां मुझे मिला समाधान है।
P12 कुंजी
आम तौर पर के लिए, हम एक टोकन उत्पन्न करने के लिए निम्न कोड का उपयोग करें।
public static function getToken()
{
$key = file_get_contents(APPLICATION_DIR.'/'.APPLICATION_GOOGLE_CLIENT_KEY_FILE);
$cred = new Google_Auth_AssertionCredentials(
APPLICATION_GOOGLE_CLIENT_EMAIL,
array('https://spreadsheets.google.com/feeds'),
$key
);
$client = new Google_Client();
$client->setAssertionCredentials($cred);
if (!$client->getAuth()->isAccessTokenExpired()) {
return false;
}
else {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$service_token = json_decode($client->getAccessToken());
return $service_token->access_token;
}
JSON कुंजी के लिए
लेकिन अब हम P12 कुंजी नहीं है, हम एक JSON कुंजी है, तो मैं बस उपरोक्त कोड में कुछ परिवर्तन किए, कृपया नीचे एक नज़र डालें:
public static function getToken()
{
$key = APPLICATION_DIR.'/'.APPLICATION_GOOGLE_CLIENT_KEY_FILE;
$data = json_decode(file_get_contents($key)); // here i decoded the json
if (isset($data->type) && $data->type == 'service_account') {
$cred = new Google_Auth_AssertionCredentials(
APPLICATION_GOOGLE_CLIENT_EMAIL, // it's the client email
array('https://spreadsheets.google.com/feeds'), // it's google spreadsheet scope
$data->private_key // here is the private key
);
}
$client = new Google_Client();
$client->setAssertionCredentials($cred);
if (!$client->getAuth()->isAccessTokenExpired()) {
return false;
}
else {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$service_token = json_decode($client->getAccessToken());
return $service_token->access_token;
}
स्रोत
2015-12-10 05:09:24
क्या आपने JSON कुंजी बनाने के निर्देशों का पालन किया था? – tam5
क्या आप अपना कार्यान्वयन दिखा सकते हैं? – yergo
@ इन्सोमैनिया क्या आप अभी भी इस पर काम कर रहे हैं? – Twisty