function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Carl HCarl H 

Google Spreadsheets API authentication

Hi all,

I'm trying to make a connection to google Spreadsheet with the example provided in (http://wiki.apexdevnet.com/index.php/Google_Data_Authentication)
And i'm able to get the session token but then when i trying to get the spreadsheets list with the following code

string token = ApexPages.currentPage().getParameters().get('token');
string sessToken = AuthSubUtil.exchangeForSessionToken(token);

SpreadsheetService service = new SpreadsheetService();
service.setAuthSubToken(sessToken);
GoogleData sheets = service.getSpreadsheets();

But the system return me this error = "Illegal character(s) in message header value: AuthSub token="CJm1yZSmDhDemoas-f____8B "" and the "CJm1yZSmDhDemoas-f____8B" actually is my session token return from AuthSubUtil.exchangeForSessionToken(token)

string token = ApexPages.currentPage().getParameters().get('token');
string sessToken = AuthSubUtil.exchangeForSessionToken(token);

SpreadsheetService service = new SpreadsheetService();
service.setAuthSubToken(sessToken);

Then i removed the "service.getSpreadsheets()" line (as above) and print the content of service and it return me below are the message


SpreadsheetService:[
canCallout=null,
debug=null,
response=null,
service=GoogleService:[AuthSubToken=CJm1yZSmDhDCwv-BAw ,canCallout=null,debug=0,gsessionid=null,response=null,responseXml=null,slug=null],
spreadsheets=null,
spreadsheetsfeed=null]


I don't really understand what is the problem why i'm unable to get the spreadsheet list from my Google Account. Does anyone can help me please???

Below is the javascript code to make the token request from google side. FYR

var scope = 'http://spreadsheets.google.com/feeds/spreadsheets/private/full/';
var forceReDirect = '/_ui/core/google/GoogleAuthSubCallback—url=' ;
var googleRedir = 'https://www.google.com/accounts/AuthSubRequest–next=';

var myApp = encodeURIComponent(window.location.pathname + "˜id={!$CurrentPage.parameters.id}" );
var nextUrl = window.location.protocol + '//' + window.location.host + forceReDirect + myApp ;
var tokenRequestUrl = googleRedir + encodeURIComponent( nextUrl ) + "&session=1&secure=0&scope="+ scope;

 



Message Edited by Carl H on 06-27-2008 05:53 AM

Message Edited by Carl H on 06-27-2008 06:12 AM
Ron HessRon Hess
Looks like there is a trailing space on the end of that token, perhaps i am not trimming the newline, normally i save the token, which for some reason does trim the new line.

here is a new copy of the method that parses the token, should be an improvement ( authsubutil.cls )
Code:
    // Parses and returns the AuthSub token returned by Google on a successful AuthSub login request.
    public static String    getTokenFromReply(String bodyOrUrl) {
        string[] atoken = bodyOrUrl.split('=');
        if ( atoken.size() != 2) { 
            system.debug( 'invalid token, or response from AuthSubSessionToken, no token');
            return null;
        }
        system.debug('session token is: '+atoken[1].trim());
        return atoken[1].trim();
    }

 


Note, with AuthSub you can only get 10 valid tokens from google ( at one time), so generating a new one each time may be limiting, unless they are properly revoked ( by your app).  If you do revoke them, then each visit to your app will require a new login to google. I'm concerned if someone gains a token, then navigates away from your app, will the token ever be revoked?

if you store the token somewhere and then make the call will that work?



Message Edited by Ron Hess on 06-27-2008 09:22 AM

Message Edited by Ron Hess on 06-27-2008 09:28 AM
Ron HessRon Hess
It looks like the scope variable is not correct, you must use the scope string defined for this api.

the correct scope var for all spreadsheet authsub tokens is :

http://spreadsheets.google.com/feeds/


Carl HCarl H
Hi Ron,

Thank you very much. It is working now after i have removed the empty space behind the token return from AuthSUbUtil.ExhangeForSessionToken and also put in the correct scope URL.

"If you do revoke them, then each visit to your app will require a new login to google. I'm concerned if someone gains a token, then navigates away from your app, will the token ever be revoked? if you store the token somewhere and then make the call will that work?" >> It won't be revoked, and it is still valid, but i dont know how long it valid for.




Message Edited by Carl H on 07-03-2008 08:27 AM