2017-04-04 14 views
6

मैं गूगल के माध्यम से स्प्रेडशीट cakephp.I में ड्राइव अपलोड करने के लिए और उपयोग करने के लिए sheets.Code इस प्रकार है की जरूरत है google-api-php-client उपयोग कर रहा हूँ टोकन का उपयोग उत्पन्न करने के लिए php-google-spreadsheet-client 'पहुँच टोकन अमान्य है':स्प्रेडशीट पुस्तकालय php में त्रुटि

function test() { 
    require_once '../vendors/google-api-php-client-2.1.1/vendor/autoload.php'; 
    $client = new \Google_Client(); 
    $client->setApplicationName("spreadsheet"); 
    $client->setDeveloperKey("//My developerkey"); 
    $client = new Google_Client(); 
    $client->setAuthConfig('client_id.json'); 
    if(!isset($_GET['code'])) { 

     $client->addScope(Google_Service_Drive::DRIVE); 
     $client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/test'); 
     $auth_url = $client->createAuthUrl(); 
     header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL)); 
    } else { 

     $client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/test'); 
     $auth_url = $client->createAuthUrl(); 
     $token = $client->fetchAccessTokenWithAuthCode($_GET['code']); 
     $client->setAccessToken($token); 

     $serviceRequest = new Google\Spreadsheet\DefaultServiceRequest($token['access_token']); 
     Google\Spreadsheet\ServiceRequestFactory::setInstance($serviceRequest); 
     $spreadsheetService = new Google\Spreadsheet\SpreadsheetService(); 
     $spreadsheetFeed = $spreadsheetService->getSpreadsheetFeed(); 
     $spreadsheet = $spreadsheetFeed->getByTitle('Test'); 
    } 

लेकिन मैं के रूप में निम्नलिखित छवि में दिखाया गया है कि टोकन का उपयोग त्रुटि अमान्य है मिलती है:

enter image description here

क्या यह है कि मैं गलत कर रहा हूँ है?

+0

@johan हिरण, आप एक बार देख हो सकता है https://github.com/google/google-api: ६५१०४०३२१० पहली बार जब आप नमूना चलाने के लिए, यह आप पहुंच अधिकृत करने का संकेत देगा -php-client/issues/1157 – tarikul05

उत्तर

0

Google स्प्रेडशीट के साथ कई मुद्दों का सामना करने के बाद मैंने अंततः यह किया। अपने डेवलपर कंसोल में web_service खाता बनाने और जो अपने वर्ग/फ़ाइल में

{ 
     "type": "service_account", 
     "project_id": "rank********er", 
     "private_key_id": "*****************************", 
     "private_key": "-----BEGIN PRIVATE KEY-----\nkey here\n", 
     "client_email": "rank***@appspot.gserviceaccount.com", 
     "client_id": "1066978********************", 
     "auth_uri": "https://accounts.google.com/o/oauth2/auth", 
     "token_uri": "https://accounts.google.com/o/oauth2/token", 
     "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", 
     "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/ra*******r%40appspot.gserviceaccount.com" 
    } 

तब के समान दिखता है, जहां आप उपयोग करने के लिए गूगल स्प्रेडशीट इस कोड का उपयोग चाहते .json प्रारूप में अपनी साख डाउनलोड करें। स्प्रेडशीट का उपयोग करने से पहले आपको उस शीट को जेसन फ़ाइल में दिए गए क्लाइंट-ईमेल पर साझा करने की आवश्यकता है।

<?php 
include dirname(__DIR__) . "/vendor/autoload.php"; 

use Google\Spreadsheet\DefaultServiceRequest; 
use Google\Spreadsheet\ServiceRequestFactory; 

$credential = dirname(__FILE__) . "/client_secret.json"; 
putenv('GOOGLE_APPLICATION_CREDENTIALS=' . $credential); 
$client = new \Google_Client; 
$client->useApplicationDefaultCredentials(); 

$client->setApplicationName("Some App"); 
$client->setScopes([ 
    'https://www.googleapis.com/auth/drive', 
    'https://spreadsheets.google.com/feeds' 
    ]); 

if ($client->isAccessTokenExpired()) { 
    $client->refreshTokenWithAssertion(); 
} 
$accessToken = $client->fetchAccessTokenWithAssertion()["access_token"]; 

$serviceRequest = new DefaultServiceRequest($accessToken); 
ServiceRequestFactory::setInstance($serviceRequest); 

$spreadsheetService = new Google\Spreadsheet\SpreadsheetService(); 
$spreadsheetFeed = $spreadsheetService->getSpreadsheetFeed(); 
$spreadsheet = $spreadsheetFeed->getByTitle('Spreadsheet Name'); 
$worksheet = $spreadsheet->getWorksheetFeed()->getEntries()[0]; 

$listFeed = $worksheet->getListFeed(); 
$cellFeed = $worksheet->getCellFeed(); 
$entries = $listFeed->getEntries(); 

foreach ($listFeed->getEntries() as $entry) { 
    $proxy = $entry->getValues(); 
    $proxy['status'] = $status_message; 
    $entry->update($proxy); 
} 

शीर्षक/ऐप नाम को आपके अनुसार बदलें। आप कोड की उपरोक्त पंक्तियों का उपयोग करने के लिए संगीतकार के माध्यम से नीचे पुस्तकालयों को स्थापित कर सकते हैं।

"google/apiclient":"^2.0", 
"asimlqt/php-google-spreadsheet-client": "3.0.*", 

संदर्भ उपयोग के लिए: https://github.com/asimlqt/php-google-spreadsheet-client

+0

कोड में मुझे स्प्रेडशीट पढ़ने के लिए Google खाता चुनने के लिए कहा जाएगा? –

+0

getEntries का उपयोग स्प्रेडशीट –

+0

से प्रविष्टियों को पढ़ने के लिए किया जाता है। मैं एक Google खाता प्रॉम्प्ट के बारे में बात कर रहा हूं जैसे http://www.awesomescreenshot.com/image/2363873/9aba733d47e63321d02f0368baa3df3e –

1

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

किसी और चीज की

इस त्वरित प्रारंभ चलाने के लिए आपको आवश्यकता होगी:

पीएचपी 5.4 या कमांड लाइन इंटरफेस (CLI) और JSON एक्सटेंशन भी इंस्टॉल साथ अधिक से अधिक। संगीतकार निर्भरता प्रबंधन उपकरण। इंटरनेट और वेब ब्राउज़र तक पहुंच। एक Google खाता।

चरण 1: Google शीट एपीआई

बना सकते हैं या Google डेवलपर कंसोल में एक परियोजना का चयन करें और स्वचालित रूप से एपीआई चालू करने के लिए इस विज़ार्ड का उपयोग करें चालू करें। जारी रखें पर क्लिक करें, फिर प्रमाण पत्र पर जाएं। अपने प्रोजेक्ट पेज पर क्रेडेंशियल जोड़ें पर, रद्द करें बटन पर क्लिक करें। पृष्ठ के शीर्ष पर, OAuth सहमति स्क्रीन टैब का चयन करें। एक ईमेल पता का चयन करें, यदि पहले से सेट नहीं है, तो उत्पाद का नाम दर्ज करें, और सहेजें बटन पर क्लिक करें। प्रमाण-पत्र टैब का चयन करें, प्रमाण-पत्र बनाएं बटन पर क्लिक करें और OAuth क्लाइंट आईडी का चयन करें। एप्लिकेशन का प्रकार चुनें अन्य, "Google शीट्स एपीआई क्विकस्टार्ट" नाम दर्ज करें, और बनाएं बटन पर क्लिक करें। परिणामस्वरूप संवाद को खारिज करने के लिए ठीक क्लिक करें। क्लाइंट आईडी के दाईं ओर फ़ाइल_डाउनलोड (JSON डाउनलोड करें) बटन पर क्लिक करें। इस फ़ाइल को अपनी कार्य निर्देशिका में ले जाएं और इसे client_secret.json का नाम दें।

चरण 2: गूगल ग्राहक लाइब्रेरी

स्थापित करें निम्न कमांड चलाएं संगीतकार का उपयोग कर पुस्तकालय स्थापित करने के लिए:

php composer.phar require google/apiclient:^2.0 

वैकल्पिक स्थापना विकल्पों के लिए लाइब्रेरी की स्थापना पेज देखें। चरण 3: नमूना

सेट अप अपने काम निर्देशिका में quickstart.php नाम की एक फ़ाइल बनाएँ और निम्न कोड में कॉपी:

<?php 
require_once __DIR__ . '/vendor/autoload.php'; 


define('APPLICATION_NAME', 'Google Sheets API PHP Quickstart'); 
define('CREDENTIALS_PATH', '~/.credentials/sheets.googleapis.com-php-quickstart.json'); 
define('CLIENT_SECRET_PATH', __DIR__ . '/client_secret.json'); 
// If modifying these scopes, delete your previously saved credentials 
// at ~/.credentials/sheets.googleapis.com-php-quickstart.json 
define('SCOPES', implode(' ', array(
    Google_Service_Sheets::SPREADSHEETS_READONLY) 
)); 

if (php_sapi_name() != 'cli') { 
    throw new Exception('This application must be run on the command line.'); 
} 

/** 
* Returns an authorized API client. 
* @return Google_Client the authorized client object 
*/ 
function getClient() { 
    $client = new Google_Client(); 
    $client->setApplicationName(APPLICATION_NAME); 
    $client->setScopes(SCOPES); 
    $client->setAuthConfig(CLIENT_SECRET_PATH); 
    $client->setAccessType('offline'); 

    // Load previously authorized credentials from a file. 
    $credentialsPath = expandHomeDirectory(CREDENTIALS_PATH); 
    if (file_exists($credentialsPath)) { 
    $accessToken = json_decode(file_get_contents($credentialsPath), true); 
    } else { 
    // Request authorization from the user. 
    $authUrl = $client->createAuthUrl(); 
    printf("Open the following link in your browser:\n%s\n", $authUrl); 
    print 'Enter verification code: '; 
    $authCode = trim(fgets(STDIN)); 

    // Exchange authorization code for an access token. 
    $accessToken = $client->fetchAccessTokenWithAuthCode($authCode); 

    // Store the credentials to disk. 
    if(!file_exists(dirname($credentialsPath))) { 
     mkdir(dirname($credentialsPath), 0700, true); 
    } 
    file_put_contents($credentialsPath, json_encode($accessToken)); 
    printf("Credentials saved to %s\n", $credentialsPath); 
    } 
    $client->setAccessToken($accessToken); 

    // Refresh the token if it's expired. 
    if ($client->isAccessTokenExpired()) { 
    $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken()); 
    file_put_contents($credentialsPath, json_encode($client->getAccessToken())); 
    } 
    return $client; 
} 

/** 
* Expands the home directory alias '~' to the full path. 
* @param string $path the path to expand. 
* @return string the expanded path. 
*/ 
function expandHomeDirectory($path) { 
    $homeDirectory = getenv('HOME'); 
    if (empty($homeDirectory)) { 
    $homeDirectory = getenv('HOMEDRIVE') . getenv('HOMEPATH'); 
    } 
    return str_replace('~', realpath($homeDirectory), $path); 
} 

// Get the API client and construct the service object. 
$client = getClient(); 
$service = new Google_Service_Sheets($client); 

// Prints the names and majors of students in a sample spreadsheet: 
// https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit 
$spreadsheetId = '1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms'; 
$range = 'Class Data!A2:E'; 
$response = $service->spreadsheets_values->get($spreadsheetId, $range); 
$values = $response->getValues(); 

if (count($values) == 0) { 
    print "No data found.\n"; 
} else { 
    print "Name, Major:\n"; 
    foreach ($values as $row) { 
    // Print columns A and E, which correspond to indices 0 and 4. 
    printf("%s, %s\n", $row[0], $row[4]); 
    } 
} 

चरण 4: नमूना चलाने

निम्न आदेश का उपयोग कर नमूना चलाएं:

php quickstart.php 
Browse to the provided URL in your web browser. 

If you are not already logged into your Google account, you will be prompted to log in. If you are logged into multiple Google accounts, you will be asked to select one account to use for the authorization. 
Click the Accept button. 
Copy the code you're given, paste it into the command-line prompt, and press Enter. 

More about google sheets

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