Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google plus sign in button not rendered when online

I'm developing a community site for my new mobile game and I'm trying to add the google plus sign in flow to it.


Steps taken during implementation:

-> Followed this google developers tutorial

HTML button code:

     <span id="signinButton">
        <span
            class="g-signin"
            data-callback="onSignInCompleted"
            data-clientid="<myID>.apps.googleusercontent.com"
            data-cookiepolicy="single_host_origin"
            data-requestvisibleactions="http://schemas.google.com/AddActivity"
            data-scope="https://www.googleapis.com/auth/plus.login">
        </span>
    </span>

Javascript load code: (just before body closure)

    <script type="text/javascript">
      (function() {
       var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
       po.src = 'https://apis.google.com/js/client:plusone.js';
       var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
     })();
</script>

The problem:

-> The implementation works fine in localhost

-> The implementation doesn't work online (i.e. uploaded on the server)


Online page strange behavior:

-> The button doesn't get rendered

-> The signed callback doesn't get called


My web application OAuth 2.0 javascript origins:

https://fcouceiro.com
https://www.fcouceiro.com
http://www.fcouceiro.com
http://fcouceiro.com
http://meatify:8888

I'm currently using fcouceiro.com as origin, but the idea is to use meatify.fcouceiro.com subdomain after this problem is solved.


UPDATE: after some experiments i've found that sometimes (like the first time you're visiting the page) it does get rendered (the button). So I'm thinking the problem clould be the script being loaded after the html for instance. As I'm using the jQuery load function to load an html page that contains the sign in button. And than the onload gets called and the script tries to find my button but it isn't available. What do you think?

You can find my page here: http://fcouceiro.com

APPy coding!

like image 336
couceirof Avatar asked Jan 14 '14 15:01

couceirof


People also ask

How do I get a Google sign in button?

To create a Google Sign-In button with custom settings, add an element to contain the sign-in button to your sign-in page, write a function that calls signin2. render() with your style and scope settings, and include the https://apis.google.com/js/platform.js script with the query string onload=YOUR_RENDER_FUNCTION .

How do I add a Google sign in HTML?

Add the library + credentials + button to your HTML The script tag grabs the library from Google that reads your <meta> tag to use as the Client ID, and then automatically re-styles the button with the CSS class . g-signin2 . At this point, if you refresh your page, you should see a pretty Sign-In button.

What is a Google button?

A personalized button displays profile information for the first Google session that approves your website. An approved Google session has a corresponding account on your website who has signed in via Sign In With Google before.


3 Answers

Ok guys. Problem solved! Thanks for your help. Here is the solution (complex one...):

Just make sure that the javascript for the google + sign in button is loaded after the button is "loaded" (if you use the html render method). My problem was that my sign in button was loaded by jQuery load function into a div and sometimes this function takes some time to execute. Fortunately there is a callback argument in that function that I've set up to load my g+ js!

Thank you again!

like image 179
couceirof Avatar answered Nov 14 '22 22:11

couceirof


To clarify the chosen answer:

The platform.js must be loaded before you render the sign-in button, for example in angularjs, you could simply use $timeout to wait for 1 sec.

like image 36
iceNuts Avatar answered Nov 14 '22 22:11

iceNuts


For anyone else who might get tangled with this,data-cookiepolicy="single_host_origin" might be the cause of your problems.

Just try clearing your cookies or setting data-cookiepolicy="none" while developing.

A more in depth description of date-cookiepolicy can be found here: https://developers.google.com/+/web/signin/reference

like image 24
Magarusu Avatar answered Nov 14 '22 23:11

Magarusu