2012-08-10 17 views
7

मैं उपयोगकर्ता लॉगिन और प्राधिकरण को लागू करने के लिए "google-api-php-client" -Library (http://code.google.com/p/google-api-php-client/) में दिए गए उदाहरणों का उपयोग कर रहा हूं Google सेवाओं के साथ मेरी वेबसाइट पर। मैंने अपने क्लाइंट-आईडी को जोड़ने के अलावा, उदाहरणों में कोई बदलाव नहीं किया है ..google api (OAuth 2) अनुमतियों को कैसे स्टोर करें?

प्राधिकरण स्वयं ठीक काम करता है: उपयोगकर्ता लॉगिन कर सकते हैं और मैं प्रदान की गई जानकारी प्राप्त कर सकता हूं। हालांकि, पृष्ठ छोड़ते समय, संपूर्ण प्राधिकरण प्रक्रिया को फिर से बुलाया जाता है; उपयोगकर्ताओं को याद नहीं किया जाता है और उन्हें फिर से अनुमति देने की आवश्यकता होती है, जो कुछ प्रकार की परेशान होती है और Google-logins के लिए विशिष्ट नहीं है क्योंकि मैं उन्हें जानता हूं।

उदाहरण के लिए: स्टैक ओवरफ्लो पर, मैं अपने Google खाते से लॉग इन हूं। जब भी मैं इस साइट पर फिर से जाता हूं, मैं स्वचालित रूप से लॉग इन हूं, या (यदि लॉग आउट हो गया है) को फिर से लॉग इन करना होगा - मुझे फिर से सामान्य अधिकारों की पुष्टि करने की आवश्यकता नहीं है। हालांकि मेरी साइट पर उदाहरणों का उपयोग करने से, उपयोगकर्ता को साइट पर फिर से मिलने पर पहुंच की अनुमति देने के लिए मजबूर किया जाता है।

क्या मैंने उदाहरणों का उपयोग करते समय कोई गलती की थी? बार-बार अनुमति अनुरोध से बचने के लिए मुझे क्या करना है?

किसी भी तरह की मदद के लिए अग्रिम धन्यवाद!

https://developers.google.com/drive/examples/php

मूल रूप से, एक बार उपयोगकर्ता के प्रवेश और आप टोकन और ताज़ा पहुँच पुनः प्राप्त:

उत्तर

0

Google डिस्क का SDK दस्तावेज़ आरंभ करने के लिए एक पूरा पीएचपी नमूना आवेदन है कि आप एक संदर्भ के रूप में उपयोग कर सकते हैं शामिल टोकन, आप डेटाबेस में उन प्रमाण-पत्रों को संग्रहीत करते हैं और उपयोगकर्ता को हर बार प्रमाणीकृत करने के बजाय उन्हें पुन: उपयोग करते हैं।

<?php 
     require 'google-api-php-client/src/Google_Client.php'; 
     require 'google-api-php-client/src/contrib/Google_DriveService.php'; 
     require 'google-api-php-client/src/contrib/Google_Oauth2Service.php'; 
    session_start(); 

     $client = new Google_Client(); 
     $client->setClientId(CLIENT_ID); 
     $client->setClientSecret(CLIENT_SECRET); 
     $client->setRedirectUri(REDIRECT_URI); 
     $client->setScopes(array(
      'https://www.googleapis.com/auth/drive', 
      'https://www.googleapis.com/auth/userinfo.email', 
      'https://www.googleapis.com/auth/userinfo.profile')); 

     $client->setUseObjects(true); 
     $service = new Google_DriveService($client); 

    //ACCESS_TOKEN is already saved in database, is being saved on first time login. 

     $_SESSION['access_token'] = ACCESS_TOKEN; 

     if (isset($_SESSION['access_token'])) { 
      $client->setAccessToken($_SESSION['access_token']); 
     } 

     if ($client->getAccessToken()) 
     { 
      $userinfo = $service->about->get(); 
      echo '<script>console.log('.json_encode($userinfo).');</script>'; 

      $userinfoService = new Google_OAuth2Service($client); 
      $user = $userinfoService->userinfo->get(); 
      echo '<script>console.log('.json_encode($user).');</script>'; 
     } 
    ?> 
+0

धन्यवाद, क्लाउडियो! वह लिंक दिलचस्प लग रहा है। ऐसा लगता है कि मुझे किसी प्रकार की "डमीज़ के लिए टोकन" की आवश्यकता है-गाइड। :-) इस बीच मुझे पता चला कि '$ क्लाइंट-> setAccessType (" ऑनलाइन "); $ क्लाइंट-> setAprprovalPrompt ("auto"); 'समस्या का एक हिस्सा हल करता है। मैंने कुकी के रूप में टोकन क्लाइंट-साइड को स्टोर करने का प्रयास किया, लेकिन सफलता के बिना ... – Elvis

+0

अनुरोध किया गया URL इस सर्वर पर नहीं मिला था। – yesitsme

1

पहली बार के लिए इस कोड का प्रयोग करें access_code निकालते हैं और डेटाबेस में सहेजने के लिए। कौशल के उत्तर के आधार पर:

<?php 
require_once 'globals.php'; 
require_once 'google-api-php-client/src/Google_Client.php'; 
require_once 'google-api-php-client/src/contrib/Google_DriveService.php'; 

$client = new Google_Client(); 

// Get your credentials from the APIs Console 
$client->setClientId('YOUR_ID'); 
$client->setClientSecret('YOUR_SECRET'); 
$client->setRedirectUri('REDIRECT_URI'); 
$client->setScopes(array('https://www.googleapis.com/auth/drive')); 


$service = new Google_DriveService($client); 
$client->setUseObjects(true); 

//if no token in the session 
if ($_SESSION['google_token'] == '') { 
    //get stored token from DB 
    $sToken = $oDb->getOne("SELECT `google_token` FROM `users` WHERE `u_id` = " . (int)$_SESSION['user_id']); 
    //if no stored token in DB 
    if ($sToken == '') { 
     //autentificate user 
     $client->authenticate(); 
     //get new token 
     $token = $client->getAccessToken(); 
     //set token in session 
     $_SESSION['google_token'] = $token; 
     // set token in DB 
     $oDb->Query("UPDATE `users` SET `google_token`='$token' WHERE `u_id` = " . (int)$_SESSION['user_id']); 
    } else { 
     $_SESSION['google_token'] = $sToken; 
    } 
} 
$client->setAccessToken($_SESSION['google_token']); 

//do what you wanna do with clients drive here 
?> 
+0

अगर मैं इसके बजाय रीफ्रेश टोकन स्टोर करना चाहता था, क्योंकि जब मैं लॉग इन नहीं करता हूं, तो पृष्ठभूमि में एपीआई को कॉल करना चाहता हूं, यह चीजों को कैसे बदलता है। – Mikeys4u

1

वह मेरे लिए ठीक काम करता है: एक बार ACCESS_TOKEN करने के लिए डेटाबेस परिवर्तन कोड में सहेजा गया है

<?php 
    require 'google-api-php-client/src/Google_Client.php'; 
    require 'google-api-php-client/src/contrib/Google_DriveService.php'; 
    require 'google-api-php-client/src/contrib/Google_Oauth2Service.php'; 
    session_start(); 

    $client = new Google_Client(); 
    $client->setClientId(CLIENT_ID); 
    $client->setClientSecret(CLIENT_SECRET); 
    $client->setRedirectUri(REDIRECT_URI); 
    $client->setScopes(array(
     'https://www.googleapis.com/auth/drive', 
     'https://www.googleapis.com/auth/userinfo.email', 
     'https://www.googleapis.com/auth/userinfo.profile')); 

    $client->setUseObjects(true); 
    $service = new Google_DriveService($client); 
      $client->authenticate(); 
      $_SESSION['token'] = $client->getAccessToken(); 
      const ACCESS_TOKEN=$_SESSION['token']; 
       //code here to save in database 
    ?> 

: