Giter Site home page Giter Site logo

Comments (14)

appadmindelhi avatar appadmindelhi commented on May 12, 2024 1

I can print the user information if i put the below code on my localhost(Using Xampp Server)

$oAuth = new Google_Service_Oauth2($client);
$userData=$oAuth->userinfo_v2_me->get();
echo "<pre>";
print_r($userData);
echo "</pre>";

But can not print the user details when i have moved my code from local to live server.

I don't know what is the issue.

from google-api-php-client.

nyaaao avatar nyaaao commented on May 12, 2024

I see the same behaviour and so far narrowed it down to the Oauth service's scopes not being properly registered with Google_Client. This should be done by the Google_Client::prepareScopes() which is called from Google_Client::createAuthUrl().

In the old library there was a Google_Client::prepareService, which was called from Google_Client::createAuthUrl() as well as Google_Client::authenticate().

I'm still digging this, but there's a workaround to this:
you can call Google_Client->setScopes('') before a call to createAuthUrl() to explicitly set the scopes.
This way you will be able to get the info.

from google-api-php-client.

alvarolb avatar alvarolb commented on May 12, 2024

Ok, I have set the following scopes declared by default in OAuth2 before creating the createAuthUrl an now its working fine:

$client->setScopes(array(
"https://www.googleapis.com/auth/plus.login",
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/userinfo.profile",
"https://www.googleapis.com/auth/plus.me"
));

So there is a bug by automatically setting the scopes in client when adding an OAuth2 service to client. By default this request requires more user information like plus circles and other google plus information, what does not happen with previous OAuth2 version.

from google-api-php-client.

alvarolb avatar alvarolb commented on May 12, 2024

I found the problem, I have proposed a pull request in:

#7

It was a little bug when adding serviceScopes to requestedScopes...

from google-api-php-client.

nyaaao avatar nyaaao commented on May 12, 2024

@alvarolb , you beat me to it ^_^; I was about to send exactly the same pull request myself...

from google-api-php-client.

alvarolb avatar alvarolb commented on May 12, 2024

@nyaaao I fix that thanks to your help =)
I dont know how this bug is present in this version... there are 51 calls to addService with availableScopes! Some of them contains only one scope, so there is no problem, but many of them contains more than one or 0! A little bug that affects all services...

from google-api-php-client.

ianbarber avatar ianbarber commented on May 12, 2024

This was actually somewhat intentional, as the scopes are somewhat inconsistently documented - we considered not doing any automatic scope retrieval because it regularly doesn't deliver what is expected. What was being done in 0.6 was guessing at the scope based on service name, which didn't aways work either.

For example, the userinfo APIs do document those four scopes, but there is both redundancy and over-scoping in there. Requesting all possible scopes would definitely make the calls work, but would also lead to consent dialogs with more scopes than are really required.

My inclination is to go back to removing automatic scope selection entirely, as, for this API in particular, there is no decent single scope choice. You probably want userinfo.email and userinfo.profile, but not the plus scopes, for example. For most of the other APIs I suspect selecting the shortest scope will give a sane default, but that would give plus.me in this case, which would only return the user ID.

from google-api-php-client.

alvarolb avatar alvarolb commented on May 12, 2024

I now understand the problem. In this case I would propose a simple approach:

In order to keep the code as is I would add a method in Google_Service called 'setRequestedScopes' with an array of strings. This method can check that the given requested scopes are within initialized available scopes to avoid user submitting invalid scopes for the given service. Also, as the client is yet initialized in Google_Service by its child class, automatically call a method on Google_Client called setServiceScopes filing the $requestedScopes similar to addService, maybe with the same parameters. Then, in prepareScopes iterate over requestedScopes instead of available availableScopes, so in this case are only filled the requested scopes. In this case I do not see necessary the addService method (and availableScopes) in Google_Client as it would contain redundant information and only is used, apparently, in prepareScopes.

So in this way each service will contain it set of availableScopes and the client a set of requestedScopes for each added service as the user requirements. This would require documentation about scopes for each service...

from google-api-php-client.

ianbarber avatar ianbarber commented on May 12, 2024

Hmm, I'm not sure we should be automatically selecting scopes at all. The simplest solution from a library point of view would be to remove the defaulting of any scopes, and require that they are manually set. This is how several of the other client libraries work. That would also remove the need to do addService.

The previous version of the client library basically guessed, by setting a scope based on the name of the service. This worked in some cases, not in others, and caused too much access in others.

The current strategy of selecting just the first scope clearly has issues (e.g. the most generic YouTube scope is in the middle of the bunch), but requesting all scopes wouldn't fit either. @alvarolb regarding your suggestion, if the user is going to the trouble to request setRequestedScopes, then they may as well do that on the client directly. How about:

  1. No scopes are requested automatically
  2. We add a new addScope method to the client which will add the scope and automatically supress duplicates

To make life easier we could look at adding friendly constant names for scopes (e.g. Google_Service_YouTube::SCOPE_YOUTUBE_READONLY) though writing it out now it doesn't look much shorter! We could also check that there are services connected which provide the scopes, to try and avoid typo'd scopes.

We could also make this a configurable behaviour. Overall though, I think requiring the scope to be specified is the only really sustainable choice.

from google-api-php-client.

silvolu avatar silvolu commented on May 12, 2024

I agree with Ian, requiring the scope to be specified is the only really sustainable choice.

from google-api-php-client.

alvarolb avatar alvarolb commented on May 12, 2024

I also agree, there should be some way of selecting desired scopes, so no default scopes are selected by default.

from google-api-php-client.

nyaaao avatar nyaaao commented on May 12, 2024

If we're removing the Google_Client->addService(), can we do something about the service initialization, i.e. the restriction when initializing with an authenticated Google_Client? Currently we may obtain the access token from somewhere else, skip the Google_Client->authenticate() part and use Google_Client->setAccessToken() right away, effectively bypassing the restriction and getting a 401: Unauthorized Request in the face if the token did not grant the access to the API. If the developer would be responsible for getting the right token and there is nothing else in the library that depends on this, we may safely remove this check altogether.

from google-api-php-client.

ianbarber avatar ianbarber commented on May 12, 2024

Closing this one for now - there's a bit of tidy up pending the next batch of template changes, but the move to manually specifying scopes is in. As for the next template change we'll have some friendlier constant names that can be references for the scopes as well (though they're attached to our current long class names, so the benefit is cognitive rather than finger mileage!).

from google-api-php-client.

bichitra12 avatar bichitra12 commented on May 12, 2024

i am not getting name for some user please help

from google-api-php-client.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.