i was wondering, the documentation has tutorials for implementing use of the Analytics API in several languages.
Now in PHP they show how to store the access token and maintain it , now i assume the JS somehow mentains it in some sort of local storage but i don’t wish the user to authenticate each time he visitis so my plan is to save the access & refresh token in my database and simply applying it to the client-side instead of going through the all pop up procress.
According to tutorial this :
gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, result);
Fires the popup which returns the access token but i’ll say again i’m intrested in submiting token from database.
How can that be done?
is there some gapi.auth.setToken(token) method which after i could make calls to Core Reporting API?
I arrived here looking for a solution using this after having already written PHP to do the auth using google’s PHP client libraries. I wanted to share the stored token as you mentioned, and be able to use javascript without re-authenticating, and/or triggering the popup (there may be a solution to this using the
.init(callback)method, see docs/links at end).It turns out you can, there is the exact
setToken(token)function you mention, and, you can even share the auth token generated earlier in PHP. What I’m not sure about yet, is if we should do it 🙂I’m using PHP to do the initial auth, but presumeably in the javascript client you’d be able to call
setToken()on something that you’d stored withgetToken()in the same manner as this example. There may also be better approaches to this, like CORS (see links at end) mentioned in the API Authentication docs that I haven’t had a chance to investigate any of these yet, but, I can give an example to answer the question, and might be useful to others needing the same behaviourI first found Google developer Dan Holevoet’s blog post with some sample JS code.
http://googleappsdeveloper.blogspot.com.au/2011/12/using-new-js-library-to-unlock-power-of.html
It’s great to be able to query the API directly with javascript, and dynamically load lists etc, but the thing that worried me about this of course was storing clientid etc in js..
But, according to Dan in an answer to the same question:
Now, my example is for the calendar API, but it all seems pretty consistent with the other APIs as well.
Note: This snippet was for proof-of-concept purposes only, and probably shouldn’t be used in production. I assume the referrer protection mentioned makes something like this OK to do, but more thought needs to be given. It could be done by hidden input, AJAX call etc.. But in the end, they’re all going to be visible in the javascript.
What I did to test the concept was to:
authMe()to set the tokenmakeApiCall())Like so:
In the php callback routine, regardless of whether authenticated yet (assuming that your callback URL is the same script), make this var global
Now, in php callback routine once we’ve checked that we’re authenticated, and
$_SESSION['token'] = $client->getAccessToken();has been called (IE storing the auth token somewhere for later), or at least$client->getAccessToken()has something meaningful:Note: I used jquery in my example to quickly parse the JSON, we’re already using it in the project, but if not, you’ll have to find another library to do so
Relevant/useful docs:
Let me know if anything’s unclear, and can post a working sample to demo.