64 thoughts on “Authentication and Authorization for Google APIs in Javascript popup window – Tutorial

  1. Hi,
    Thanks for the post. I have used it it works perfect for my application.

    I need to users to be logged out of google when they logout of my application. How do I achieve this? Please help.

  2. Thanks to all for your appreciation. I added logout functionality to the code and updated the post appropriately.
    I dont have Window in both home and office, thats why I cant look into IE issue. But will try to fix it somewhere this weekend and update the post.
    Thanks :-)

  3. Thanks for the tutorial.

    Your Demo is not working in Safari. It returns “Welcome undefined”.

    I’m using OSX 10.6.8. The Demo is working in Firefox and Chrome.

  4. Fixed the bug in Safari. It was a missing slash at the end of REDIRECT url: ‘http://localhost:8888/MAMP/web-prototypes/googleapi/oauth/’

    Thanks Bhriv for pointing it out :-)

  5. Saiyasodharan R, I posted this same question in stackoverflow.

    If I get access token the way you suggest, which will make it valid in parent window, can I then make API calls to google apps using the google-api-php-client library?

    Thank you,

    Veronica

  6. @Veronica: Since you are using PHP library, I suggest you to authorize using the functions provided by that library itself rather than falling back to JS for authentication. I believe that is possible. and I replied the same in SO.

  7. I have set up a a Project in the Google APIs Console.
    I have followed your example.
    I have set up client ID.
    I have created a blank page in root directory of Tomcat, and set the corresponding redirect uri
    Redirect URIs: https://localhost:8080/test2.html

    I get this error:
    Secure Connection Failed

    An error occurred during a connection to localhost:8080.

    SSL received a record that exceeded the maximum permissible length.

    (Error code: ssl_error_rx_record_too_long)

    The page you are trying to view cannot be shown because the authenticity of the received data could not be verified.
    Please contact the website owners to inform them of this problem. Alternatively, use the command found in the help menu to report this broken site.

    What can be the reason????
    Thanks much in advance!!!!

  8. When I redirect to my blank page, I get the buttons, allow access/no thanks. But when I run with your redirect URL and client ID, it doesn’t ask for allow access. Did I do something wrong with my client registration?

  9. “run with your redirect URL” ?? are you also developing in MAMP as I do ? as long as the redirect path given in API console and the actual paths we use match, it should work…

    • I use Apache on windows and IE. Your html code as downloaded works fine. When I change the redirect URL and client ID to my own, I get the allow access question. But with yours I don’t and the token is printed on the window title.

      • As I said already, the actual Redirect URL should match the one you given in the API console and obviously my client ID and yours will be different. So, it wont work out for you with my URL and client ID…

  10. Example works fine but after login google i cant redirect to my url..i want to redirect::
    localhost/oauth/index.php and want every login user on different redirect?? its possible?? if Yes than How??

    Thank in advance..!

  11. in `getUserInfo` function, get the user’s name and then make a redirect like `window.location.href = “localhost/profile/” + user.name;`

        • Its working fine for me in Chrome v22 and I also checked it to be working in IE8. Can you tell me your browser versions ?

        • I have also struggled with the issue, until realizing that my code page was in sub domain of local host, and the redirect page was under localhost (google do not allow for some reason the redirect url to be in a subdomain of localhost). once moving the source page to localhost (to be in the same domain of the redirect page), all was ok.

          it is a sequrity issue to access info from different domains I guess (although it is just domain and subdomain)

  12. With this method, you don’t have to give to Google any secret key. Isn’t it a problem?

    I could take your code, modify it, and for every user who has accepted to use your app, access to their data because Authentification would be invisible since user already have accepted the app.

    With this testApp, maybe it’s not a real problem, but imagine you do the same thing with a well known app. If I see “well known app wants to access to your account”, if I use well known app myself, I will accept the access, and a totally unknown developer will be able to play with my data.

    To sum up, the absence of control of app identity scares me a lot, and I’m surprised that Google don’t talk about it in this page:
    https://developers.google.com/accounts/docs/OAuth2UserAgent

    It seems to me a bit too big to believe in. Where am I wrong ?

    • I guess REDIRECT URI serves this purpose. After the auth process, google redirects to the URI in ‘my domain’ that I defined in Google apis console, which you cant access from any random domain because of cross domain policy restrictions.

  13. I think this is one of the most significant info for me. And i am glad reading your article. But should remark on few general things, The web site style is ideal, the articles is really great : D. Good job, cheers

  14. Great Post!
    But how come if i test your code locally with my API data it keeps on showing the “Request for permission” page?
    Your example application works perfect, it only ask for permission once. And afterwards it always delivers the access token (until the token expires)

  15. Nice one thanks for sharing keep it up

    can we do it only using javascript i mean without jquery
    if yes please give me suggestion

    Cheers
    Anand Neema

  16. I see the code the code is nice and i am very happy its working fine but i want more information Like users bithdate,location etc there is any parameter to access that

  17. Hello,
    I am getting this error

    Unable to connect
    Firefox can’t establish a connection to the server at localhost.

    i think this is sumthing to do with redirect URL.

    if i want to run my app locally..then which URL should i mention in the API console.?

    Thanks

  18. And the thing is that.. i am making a chrome extension which uses oauth to access gmail api…so if u know sumthing..abt it..then plz help…thanks

  19. Is it necessary to hide the “redirecturl” for the user once it is successfull. OR is there any problem in showing the right “redirecturl”

  20. Hi,
    Great turorial by the way. I’m very new to JavaScript, ajax and so on. Was just wondering, on a general development level how to get this working on my local machine.

    I’ve followed the example and redirected to an existing site. Which works with the AccessToken etc appearing in the URL, but the popup doesn’t close and the browser window running your code doesn’t get the user info – presume this is due to using some random redirect URL that’s different to where the original page is located.

    So my question is, what should I set the redirect URL to (in the API console and the HTML file) in order to run this from local file on my hard drive? Do I need to setup localhost in order to get this working? or is there an easier way ?

    • Yes, a localhost is mandatory. Google servers cant redirect to a file which is in your local filesystem. They need to be accessed via http, in order for the redirection to work.
      Try WAMP / MAMP / LAMP, which is very easy to install and work with.

  21. I faced the problem to access the document property because of security , Actually If you miss “www” in any of url then it will not work… Fix it that

  22. I just finished using the material herein to implement it on my app. Thank you very much for your contribution.

    Regards
    Rodrigo

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>