google api with libcurl

612 views
Skip to first unread message

Bradd

unread,
Jul 4, 2011, 8:24:45 AM7/4/11
to google-docum...@googlegroups.com

Hi,

I am using libcurl in c++ and been able to authenticate the user (using httpauth) so far but the main problem occurs when I try to retrieve the general document list in his\her account (using httpheader) .

here is the code for the same..!!

***************************************************************************************************************************************************

string strURL = "https://www.google.com/accounts/ClientLogin";
string strHeader = "Content-type: application/x-www-form-urlencoded";
string strPost = "POST /accounts/ClientLogin HTTP/1.0";
string strUserName = "a...@gmail.com";
string strPwd = "abc";

string strConnectionFields;
strConnectionFields = "accountType=HOSTED_OR_GOOGLE&Email=" + strUserName + "&Passwd=" + strPwd + "&service=writely&source=abc-abc-1";


/*Logging in the user account with the provided credentials */
curl_easy_setopt(curl,CURLOPT_URL,strURL.c_str());
curl_easy_setopt(curl,CURLOPT_POST,true);
curl_easy_setopt(curl,CURLOPT_POSTFIELDS, strConnectionFields.c_str());
curl_easy_setopt(curl,CURLOPT_HTTPAUTH,CURLAUTH_ANY);
curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER,false); //skipping peer verification
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, handle_data); //used to redirect the output to function

res = curl_easy_perform(curl);

strAuthString = ReturnAuthString(strGoogleResponse);

string strHeaderString;

strHeaderString = "Authorization: GoogleLogin auth=" + strAuthString + " GData-Version: 3.0";

// Make the request
curl_easy_setopt(curl, CURLOPT_URL, "http://docs.google.com/feeds/default/private/full?v=3");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, strHeaderString.c_str());
curl_easy_setopt(curl, CURLOPT_POST,false);

//curl_easy_setopt(curl,CURLOPT_VERBOSE,1); //this option is used for setting the ouput to console by libcurl

res = curl_easy_perform(curl); // -> THE REAL PROBLEM OCCURS HERE..!!!

***************************************************************************************************************************************************

Please advise..

Thanks,

Bradd

Bradd

unread,
Jul 5, 2011, 6:31:13 AM7/5/11
to google-docum...@googlegroups.com
Anybody...??????????

Ali Afshar

unread,
Jul 5, 2011, 3:22:30 PM7/5/11
to google-docum...@googlegroups.com
Hi Brad,

One immediate problem is that the two headers (Authorization, and
Gdata-version) should be separated by a return character \n and not a
space character.

If there is more, can you please paste the error and the logging out
put that you got?

Regards

--
Ali Afshar | @aliafshar | Google Developer Relations

Bradd

unread,
Jul 6, 2011, 1:12:02 AM7/6/11
to google-docum...@googlegroups.com
Thanks for replying Ali.


I have included the newline character in the header string and moreover made changes for a SSL connection (as the url is a secure one "https") but instead of returning a feed containing the list of the user documents it is throwing up a bad request error . Code and error are as follows...

**************************************************************************************************************************************
//CODE//

string strHeaderString;

strHeaderString = "Authorization: GoogleLogin auth=" + strAuthString + "\nGData-Version: 3.0" ;

curl_easy_setopt(curl2, CURLOPT_URL, "https://docs.google.com/feeds/default/private/full");

curl_easy_setopt(curl2,CURLOPT_HTTPHEADER,strHeaderString);
curl_easy_setopt(curl2,CURLOPT_HTTPGET,1);
curl_easy_setopt(curl2,CURLOPT_SSL_VERIFYPEER,false); //skipping peer verification
curl_easy_setopt(curl2, CURLOPT_SSL_VERIFYHOST, 0L);


curl_easy_setopt(curl2,CURLOPT_VERBOSE,1);

res2 = curl_easy_perform(curl2);

//CODE//

**************************************************************************************************************************************

//ERROR//

* Server certificate:
*        subject: C=US; ST=California; L=Mountain View; O=Google Inc; CN=*.googl
e.com
*        start date: 2011-06-07 04:40:43 GMT
*        expire date: 2012-06-07 04:50:43 GMT
*        issuer: C=US; O=Google Inc; CN=Google Internet Authority
*        SSL certificate verify result: unable to get local issuer certificate (
20), continuing anyway.
> GET /feeds/default/private/full HTTP/1.1
Host: docs.google.com
Accept: */*

< HTTP/1.1 400 Bad Request
< Content-Type: text/html; charset=UTF-8
< Date: Wed, 06 Jul 2011 05:09:33 GMT
< Expires: Wed, 06 Jul 2011 05:09:33 GMT
< Cache-Control: private, max-age=0
< X-Content-Type-Options: nosniff
< X-Frame-Options: SAMEORIGIN
< X-XSS-Protection: 1; mode=block
< Server: GSE
< Transfer-Encoding: chunked
<
Invalid request URI* Connection #0 to host docs.google.com left intact

//ERROR//

**************************************************************************************************************************************

Thanks again for replying...!!
Bradd

Bradd

unread,
Jul 6, 2011, 2:23:00 AM7/6/11
to google-docum...@googlegroups.com
Hi Ali ,

Another update ... if I return the string pointer in the header instead of the string i.e.

curl_easy_setopt(curl2,CURLOPT_HTTPHEADER,strHeaderString.c_str());

instead of

curl_easy_setopt(curl2,CURLOPT_HTTPHEADER,strHeaderString);

there is another error occuring inside the libcurl library.

The error snapshot is attached along..

Please advise ...

Thanks,
Bradd
error.JPG

Ali Afshar

unread,
Jul 6, 2011, 4:08:42 AM7/6/11
to google-docum...@googlegroups.com
Hi Brad,

Can you get the raw request somehow, for example
http://curl.haxx.se/libcurl/c/debug.html shows how to use a callback
to get the debug information.

Regards

--

Bradd

unread,
Jul 6, 2011, 5:16:15 AM7/6/11
to google-docum...@googlegroups.com
Hi Ali,

Thanks a zillion for replying .. :)

The xml structure has been retrieved successfully for now....!

There was some mistake in the way I was passing the header information.... which is rectified now..

I will shortly upload a sample project in google code for interacting with google docs using c++,curl and an xml parser in a simple manner...

You gotta return some to the community I believe..!!!!!

Thanks a lot again for replying at such short notice...

Till next time,
Bradd.


Ali Afshar

unread,
Jul 6, 2011, 5:18:07 AM7/6/11
to google-docum...@googlegroups.com
Hi Brad,

Glad you are up and running, that C++ sample would be really useful,
so thanks a lot for doing that.

Regards

--

Reply all
Reply to author
Forward
0 new messages