2017-05-10 15 views
6

मैं Google OAuth के माध्यम से लॉग इन किए गए उपयोगकर्ता की प्रोफ़ाइल जानकारी के लिए अनुरोध करने का प्रयास कर रहा हूं। मेरा अनुरोध ठीक से बनाया गया है, मैं सफलतापूर्वक लॉग इन करता हूं, लेकिन जब मैं PHP में निम्न अनुरोध करने का प्रयास करता हूं, तो मुझे त्रुटि मिलती है अनुरोध मास्क खाली नहीं हो सकता है। मान्य पथ हैं: ...Google लोग API "अनुरोध मास्क खाली नहीं हो सकता"

हालांकि, यह Google People API people.get documentation से स्पष्ट है कि अनुरोध मास्क मान वैकल्पिक हैं, और यदि पारित नहीं किया गया है, will return all values except for people.connections.list

// The entire OAuth process works up until this point... 
// create the service 
$service = new Google_Service_People($this->client); 

try { 
    $results = $service->people->get('people/me'); 
} catch(\Exception $exception) { 
    echo $exception->getMessage(); 
    exit; 
} 

यहाँ मैं यह त्रुटि से प्राप्त अपवाद संदेश है::

{ "error": { "code": 400, "message": "Request mask can not be empty. Valid paths are: [person.addresses, person.age_ranges, person.biographies, person.birthdays, person.bragging_rights, person.cover_photos, person.email_addresses, person.events, person.genders, person.im_clients, person.interests, person.locales, person.memberships, person.metadata, person.names, person.nicknames, person.occupations, person.organizations, person.phone_numbers, person.photos, person.relations, person.relationship_interests, person.relationship_statuses, person.residences, person.skills, person.taglines, person.urls].", "errors": [ { "message": "Request mask can not be empty. Valid paths are: [person.addresses, person.age_ranges, person.biographies, person.birthdays, person.bragging_rights, person.cover_photos, person.email_addresses, person.events, person.genders, person.im_clients, person.interests, person.locales, person.memberships, person.metadata, person.names, person.nicknames, person.occupations, person.organizations, person.phone_numbers, person.photos, person.relations, person.relationship_interests, person.relationship_statuses, person.residences, person.skills, person.taglines, person.urls].", "domain": "global", "reason": "badRequest" } ], "status": "INVALID_ARGUMENT" } }

किसी को भी मेरी मदद कर सकते हैं बाहर यहाँ मेरी कोड है?


अद्यतन 1:

जब मैं अनुरोध मुखौटा के लिए कुछ मूल्य में पारित करने के लिए प्रयास करते हैं, $service->people->get('people/me', array("person.names")); मैं अपवाद संदेश मिलता है: Illegal string offset 'type'

+0

क्या आप PHP लाइब्रेरी का नवीनतम संस्करण उपयोग कर रहे हैं? यदि नहीं, तो नवीनतम संस्करण में अपडेट करने का प्रयास करें। – abraham

+1

मुझे एक ही त्रुटि मिल रही है, लेकिन जावास्क्रिप्ट और एक वेब पेज के साथ। मुझे नहीं लगता कि आखिरी बार यह काम कर रहा था, हालांकि मैंने कुछ भी बदल दिया। क्या यह आपके लिए एक नया मुद्दा है जो अभी शुरू हुआ है, या आपने केवल इसे लागू करने का प्रयास किया है? – kirypto

+1

मुझे आज से गो लाइब्रेरी के साथ एक ही त्रुटि मिलनी शुरू हुई। (मैंने कई हफ्तों तक अपने कोड को छुआ नहीं है।) शायद हाल ही में कुछ बदल गया है? – gonbe

उत्तर

4

मुझे एक काम मिल गया है। मैं लोगों की बजाय प्लस सेवा से आंशिक उपयोगकर्ता प्रोफ़ाइल जानकारी का अनुरोध करने में सक्षम था। ऐसा लगता है कि ऐसा क्यों लगता है कि Google ने अपने एपीआई के आंतरिक तर्क को संशोधित किया है, और उनके दस्तावेज़ों को अपडेट नहीं किया है। (इस लेखन के समय, Google की PHP OAuth लाइब्रेरी बीटा में है)।

मेरे मामले में, मैं वास्तव में उपयोगकर्ता के उपयोगकर्ता नाम और ईमेल पते को प्राप्त करने के बाद क्या था। प्रोफाइल अनुरोध करने के लिए People सेवा का उपयोग करने के बजाय, मैंने Plus सेवा का उपयोग किया, और ईमेल पता प्राप्त करने के लिए कुछ अतिरिक्त क्षेत्रों के लिए कहा। यहां मेरे PHP कार्यान्वयन की पूरी जानकारी है। नोट तीन स्कोप मैं निर्माता में विनती करता हूँ:

Google_Service_Plus::USERINFO_PROFILE, Google_Service_People::USERINFO_PROFILE, Google_Service_People::USERINFO_EMAIL

सफलतापूर्वक प्रमाणीकृत करने के बजाय लोगों को सेवा से people/me का अनुरोध, मैं बजाय प्लस सेवा से me विनती करता हूँ के बाद, एक जोड़ी अतिरिक्त अनुरोध प्राप्त करने के लिए के साथ शेष जानकारी:

$plus = new Google_Service_Plus($this->client); 

try { 
    $plus_results = $plus->people->get('me'); 
} catch(\Exception $exception) { 
    echo $exception->getMessage(); 
    exit; 
} 

<?php 
 

 
namespace App\Auth; 
 
require_once '/var/www/html/oauth/vendor/google/apiclient-services/src/Google/Service/People.php'; 
 
require_once '/var/www/html/oauth/vendor/google/apiclient-services/src/Google/Service/Plus.php'; 
 
require_once '/var/www/html/oauth/vendor/google/apiclient/src/Google/Client.php'; 
 
require_once 'Session.php'; 
 
use Google_Client; 
 
use Google_Service_People; 
 
use Google_Service_Plus; 
 
use App\Auth\Session; 
 

 
/** 
 
* This class performs a basic oauth authentication 
 
* using Google sign in and upon calling the handle_auth 
 
* method, retrieves the user's profile and sets session 
 
* variables for use throughout an application. 
 
*/ 
 
class GoogleAuth { 
 

 
    private static $DOMAIN = 'google'; 
 

 
    /** 
 
    * Google auth client 
 
    * @var Google_Client 
 
    */ 
 
    public $client; 
 

 
    /** 
 
    * Config json filepath 
 
    * @var String 
 
    */ 
 
    public $config_json; 
 

 
    /** 
 
    * The URI to redirect to after succesful oauth 
 
    * @var String 
 
    */ 
 
    public $redirect_uri; 
 

 
    /** 
 
    * The authorization url 
 
    * @var String 
 
    */ 
 
    public $auth_url; 
 

 
    /** 
 
    * Logout url to redirect to after logout 
 
    * @var String 
 
    */ 
 
    public $logout_url; 
 

 
    /** 
 
    * The name of the application as listed in the Google 
 
    * app Dashboard. 
 
    * @var String 
 
    */ 
 
    public $application_name; 
 

 
    /** 
 
    * The developer hash key available in the Google 
 
    * App Credentials dashboard. 
 
    * @var String 
 
    */ 
 
    public $developer_key; 
 

 
    /** 
 
    * Scopes to request in the oauth request. 
 
    * @var [type] 
 
    */ 
 
    public $scope; 
 

 
    /** 
 
    * Url to redirect to upon successful authentication 
 
    * @var String 
 
    */ 
 
    public $auth_success_url; 
 

 
    public function __construct($config) { 
 
    // Eventually we can extend the scope to handle different 
 
    // values or multiple values. For now, this class only 
 
    // supports user profile information. 
 
    $config['scope'] = array(
 
     Google_Service_Plus::USERINFO_PROFILE, 
 
     Google_Service_People::USERINFO_PROFILE, 
 
     Google_Service_People::USERINFO_EMAIL 
 
    ); 
 

 
    $this->init($config); 
 
    } 
 

 
    private function init($config) { 
 
    
 
    if(!isset($config)) { 
 
     throw new \Exception('Config is not valid.'); 
 
    } 
 
    if(!isset($config['config_json'])) { 
 
     throw new \Exception('Path to config json is invalid.'); 
 
    } 
 
    if(!file_exists($config['config_json'])) { 
 
     throw new \Exception('Config JSON file could not be found: ' . $config['config_json']); 
 
    } 
 
    if(!isset($config['application_name'])) { 
 
     throw new \Exception('Application name is invalid.'); 
 
    } 
 
    if(!isset($config['developer_key'])) { 
 
     throw new \Exception('Developer Key is invalid.'); 
 
    } 
 
    if(!isset($config['scope'])) { 
 
     throw new \Exception('Scope is invalid.'); 
 
    } 
 
    if(!isset($config['redirect_uri'])) { 
 
     throw new \Exception('Redirect URL is invalid.'); 
 
    } 
 
    if(!isset($config['logout_url'])) { 
 
     throw new \Exception('Logout URL is invalid.'); 
 
    } 
 

 
    $this->client = new Google_Client(); 
 
    $this->config_json = $config['config_json']; 
 
    $this->redirect_uri = $config['redirect_uri']; 
 
    $this->application_name = $config['application_name']; 
 
    $this->developer_key = $config['developer_key']; 
 
    $this->scope = $config['scope']; 
 
    $this->logout_url = $config['logout_url']; 
 

 
    // Let the session know where we want to go on logout. 
 
    Session::set_logout_url($this->logout_url, self::$DOMAIN); 
 

 
    $this->client->setAuthConfig($this->config_json); 
 

 
    foreach($this->scope as $scope) { 
 
     $this->client->addScope($scope); 
 
    } 
 
    
 
    $this->client->setApplicationName($this->application_name); 
 
    $this->client->setDeveloperKey($this->developer_key); 
 
    $this->client->setRedirectUri($this->redirect_uri); 
 
    $this->client->setPrompt('select_account'); 
 
    $this->auth_url = $this->client->createAuthUrl(); 
 
    } 
 

 
    public static function auth_failure(\Exception $exception) { 
 
    return Session::auth_failure(
 
     $exception->getMessage(), 
 
     self::$DOMAIN 
 
    ); 
 
    } 
 

 
    public static function logout() { 
 
    return Session::logout(self::$DOMAIN); 
 
    } 
 

 
    public function authenticate($request) { 
 
    if (!$request->has('code')) { 
 

 
     // User is unauthenticated, send them through the auth process 
 
     return filter_var($this->auth_url, FILTER_SANITIZE_URL); 
 

 
    } else { 
 
     $code = $request->input('code'); 
 

 
     // process the code received from the auth process 
 
     $token_response = $this->process_code($code); 
 
     
 
     // Ensure the token response is valid 
 
     Validator::token_response($token_response); 
 
     
 
     // Process and retrieve the access token 
 
     $raw_token = $this->process_token_response($token_response); 
 

 
     if(isset($raw_token)) { 
 
     // Handle the token and process the id_token 
 
     $this->handle_id_token($raw_token); 
 
      
 
     // Create the people service and make requests 
 
     return $this->make_profile_request(); 
 

 
     } else { 
 
     throw new \Exception('Failed to retrieve the access token'); 
 
     } 
 
    } 
 
    } 
 

 
    private function process_code($code) { 
 
    // grab the code from the URL and generate an access token 
 
    $response = $this->client->fetchAccessTokenWithAuthCode($code); 
 

 
    if(!is_array($response)) { 
 
     throw new \Exception('Token response was invalid.'); 
 
    } 
 

 
    return $response; 
 
    } 
 

 
    private function process_token_response($token_response) { 
 
    $this->client->setAccessToken($token_response); 
 
    return $this->client->getAccessToken(); 
 
    } 
 

 
    private function handle_id_token($token) { 
 

 
    $id_token = null; 
 

 
    try { 
 
     $id_token = $this->client->verifyIdToken($token['id_token']); 
 
    } catch(\Exception $exception) { 
 
     // clear the access token to disable any 
 
     // approved permissions for the user's account 
 
     $this->client->revokeToken(); 
 
     
 
     throw new \Exception('Google Login failed'); 
 
    } 
 

 
    if(!$id_token) { 
 
     throw new \Exception('Id Token is null or undefined'); 
 
    } 
 

 
    // grab the domain from the id_token 
 
    $email = $id_token['email']; 
 

 
    // Stuff it into the session 
 
    Session::set_email($email, self::$DOMAIN); 
 
    } 
 

 
    private function make_profile_request() { 
 
    // create the service 
 
    $plus = new Google_Service_Plus($this->client); 
 

 
    try { 
 
     $plus_results = $plus->people->get('me'); 
 
    } catch(\Exception $exception) { 
 
     echo $exception->getMessage(); 
 
     exit; 
 
    } 
 
    
 
    if(!$plus_results) { 
 
     throw new \Exception('No matching profile results.'); 
 
    } 
 

 
    // Get the user's display name 
 
    $username = $plus_results->getDisplayName(); 
 

 
    // Stuff it into the session 
 
    Session::set_username($username, self::$DOMAIN); 
 
     
 
    // Login. Session handles the redirect 
 
    return Session::login(
 
     $username, 
 
     Session::get_email(self::$DOMAIN), 
 
     self::$DOMAIN 
 
    ); 
 
    } 
 
} 
 
?>

+1

अपने समाधान डैनी पोस्ट करने के लिए धन्यवाद। जैसा कि मैंने उपर्युक्त उल्लेख किया है, मैं जावास्क्रिप्ट एपीआई का उपयोग कर रहा हूं, लेकिन आपका समाधान मुझे मेरा नेतृत्व करता है। अगर कोई इसे पढ़ रहा है और जानना चाहता है, तो मैंने 'लोगों' एपीआई से 'प्लस' एपीआई में भी स्विच किया। मैंने बदल दिया: 'gapi.client.people.people.get ({resourceName:" people/me "})। फिर (...)' 'gapi.client.plus.people.get में ({userId: 'me' })। फिर (...) ' – kirypto

+0

बहुत बढ़िया, मुझे खुशी है कि यह आपके लिए भी काम करता है! –

3

मैं के बाद से जाओ पुस्तकालय के साथ एक ही त्रुटि मिल रही शुरू कर दिया ~ 11 मई शामिल किए बिना मेरा कोड Google API परिवर्तन से पहले फ़ील्ड ठीक काम कर रहा था। क्षेत्र वैकल्पिक था।

Google दस्तावेज़ में, अब "includeField" फ़ील्ड आवश्यक है। मुझे कहीं और कोई घोषणा नहीं मिल रही है।

https://developers.google.com/people/api/rest/v1/RequestMask

includeField

Required. Comma-separated list of person fields to be included in the response. Each path should start with person.: for example, person.names or person.photos.

Last updated May 19, 2017

मेरी golang मामले का समाधान करने के लिए मैं People.Get कॉल करने से पहले RequestMaskIncludeField क्षेत्र प्रदान करने के लिए किया था।

people_get_call := peopleService.People.Get("people/me").RequestMaskIncludeField("person.addresses,person.age_ranges,person.biographies,person.birthdays,person.bragging_rights,person.cover_photos,person.email_addresses,person.events,person.genders,person.im_clients,person.interests,person.locales,person.memberships,person.metadata,person.names,person.nicknames,person.occupations,person.organizations,person.phone_numbers,person.photos,person.relations,person.relationship_interests,person.relationship_statuses,person.residences,person.skills,person.taglines,person.urls") 
google_account, err := people_get_call.Do() 
5

बहुत देर हो चुकी है, लेकिन शायद यह किसी और के लिए उपयोगी होगी।

दूसरा तर्क के रूप में सरणी नीचे

उपयोग API कॉल के साथ requestMask जोड़ने के लिए

$optParams = array('requestMask.includeField'=>'person.names'); 
0

तरह @gonbe ने कहा, RequestMaskIncludeField की आवश्यकता नहीं थी, लेकिन कुछ समय के लिए यह है। नवीनतम जावा lib के लिए (अब इसके rev139-1.22.0) के लिए आपको अनुरोध में जोड़ने के लिए setRequestMaskIncludeField() जोड़ने की आवश्यकता है, उदा।

peopleService.people().get("people/me").setRequestMaskIncludeField("person.email_addresses").execute(); 
संबंधित मुद्दे