Re: How can I use GTM OAuth 2 to submit/print on Google Cloud Print?

52 views
Skip to first unread message

Greg Robbins

unread,
Feb 25, 2013, 9:34:30 PM2/25/13
to gtm-o...@googlegroups.com
authorizeRequest:completionHandler: is generally all that's necessary to authorize a request, for GET and for POST. Authorizing a request with OAuth 2 just requires that the library obtain or update an access token, then put the access token into the request's Authorization header. After authorizing, you can look at the request's headers to confirm it has the authorization header.

I'm not familiar with the Cloud Print API, so I don't have specific ideas on what may be going wrong with that. A few suggestions though:

1. Consider using NSJSONSerialization to generate the JSON from an NSDictionary rather than creating the JSON with NSString. That makes the source code more readable, and avoids the chance of JSON mistakes.

2. Consider using GTMHTTPFetcher for doing the fetch, since it provides convenient logging of http requests and responses, and logs are often helpful in diagnosing these issues.


Chris

unread,
Feb 25, 2013, 10:56:10 PM2/25/13
to gtm-o...@googlegroups.com
Hi Greg,

Thank you for your fast reply and thank you for your suggestion.

1. I think NSDictionary is better but NSString is OK as well. Anyway the bodyJsonString looks ok:

bodyJsonString = {"printerid":"__google__docs","title":"tram35","capabilities":"[]","content":"http://ptv.vic.gov.au/assets/Maps/Routes/PDFs/1112_TramCityCircle.pdf","contentType":"url","tag":""}

2. Thanks I will try GTMHTTPFetcher, but I don't know what happened in GTM-OAuth2. Can any confirm that this is the right way to do POST in GTM-OAuth2? Or do I need to set headers or something?

Many thanks,
Chris

Greg Robbins

unread,
Feb 26, 2013, 1:39:20 AM2/26/13
to gtm-o...@googlegroups.com
Yes, performing authorizeRequest:completionHandler: on the mutable request with the method POST and the request's body set is the proper way to do an authenticated POST request. That will add the Authentication header to the request. 

You can confirm that the header is being added by printing [request allHTTPHeaderFields] in the callback to authorizeRequest:

Chris

unread,
Feb 26, 2013, 6:03:52 AM2/26/13
to gtm-o...@googlegroups.com
Hi Greg,

Thank you for your help.

I tried [request allHTTPHeaderFields] and there is only one field, which looks clean:

allHTTPHeaderFields = {

    Authorization = "Bearer ya29.AHES6ZSAAhwuckZt0TBkp7KdoPvVKLDVBEEimt5u0_gImXMWXjl-wXNd";

}

I have tried some headers by the reference of python sample, and some others, but still receiving errors like 400 or bad params. Does google provide any ref about api headers?

Why I asume that the POST got some problem is that the params in the response is very wired:

    params =         {
            "{\"printerid\":\"__google__docs\",\"title\":\"tram35\",\"capabilities\":\"[]\",\"content\":\"http://ptv.vic.gov.au/assets/Maps/Routes/PDFs/1112_TramCityCircle.pdf\",\"contentType\":\"url\",\"tag\":\"\"}" =             (
                ""
            );
        };

If I do it through GET, the params json string is good and everything is fine:

NSString *urlString = [NSString stringWithFormat:@"%@?printerid=%@&title=%@&capabilities=%@&content=%@&contentType=%@&tag=%@", kGoogleCloudPrintPrintingURLString, printerId, title, capabilities, content, contentType, tag];

NSURL *url = [NSURL URLWithString:urlString];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

So my question is what made Google think my params in the POST data is like that?

Many thanks,
Chris

Chris

unread,
Feb 26, 2013, 6:10:28 AM2/26/13
to gtm-o...@googlegroups.com
Sorry the header implementation in python sample code is in the HTTP Get/Post Method part

Greg Robbins

unread,
Feb 26, 2013, 4:22:03 PM2/26/13
to gtm-o...@googlegroups.com
This discussion group is just for issues related to using the gtm-oauth2 library for authentication, and it appears your problems are not related to authentication.

Cloud Print API support is available at http://groups.google.com/group/gcp-developers

I do encourage you to use GTMHTTPFetcher so you can take advantage of its http logging feature to examine the server requests and responses. The fetcher class is part of gtm-oauth2, so it's already in your project.


Chris

unread,
Feb 26, 2013, 5:17:51 PM2/26/13
to gtm-o...@googlegroups.com
Thank you for your help. I will try that first.
Reply all
Reply to author
Forward
0 new messages