REST>Google Contacts API>POST returns a bad request error (400)

I'm running Delphi XE5 (Update 2) and I'm coding against Google Contacts API, leveraging the REST components.
I've been able to authenticate against the API, leveraging OAuth2 authenticator.
I've been able to retrieve contacts by doing a GET request against the API.
I haven't been able to push data by doing a POST request against the API. I always receive a bad request error (400) from server with no additional useful detail in the response The POST request contains XML code in the body of the request.

Here's the code snippet (trying to create a Google Contacts Group here):

  RESTClient.BaseURL := 'https://www.google.com/m8/feeds';
  RESTClient.Authenticator := OAuth2_GoogleContacts;
  RESTClient.ContentType := 'application/atom+xml';

  RESTRequest.Method := TRESTRequestMethod.rmPOST;
  RESTRequest.Resource := 'groups/default/full';

  RESTRequest.Accept := 'application/atom+xml, application/json, text/plain; q=0.9, text/html;q=0.8,';
  RESTRequest.Params.AddHeader('GData-Version', '3.0');
  
  XMLText := '<?xml version="1.0" encoding="utf-8"?>' +
  '<entry xmlns:gd="http://schemas.google.com/g/2005">' +
  '<category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#group" />' +
  '<title type ="text">Foo</title>' +
  '</entry>';

  RESTRequest.AddBody(XMLText, ctAPPLICATION_ATOM_XML);
  
  RESTRequest.Execute;

I've tried multiple things with adding XML to the body (using streams, forcing UTF8 encoding, etc.) of the request but always the same bad request error.
I've checked API scope, etc. and it all looks good. Also worth to note that sending the request through Google OAuth2 Playground works well.

Thanks in advance for any help with this.
0
Jean
5/12/2015 1:20:34 PM
embarcadero.delphi.webservices 976 articles. 0 followers. Follow

7 Replies
2193 Views

Similar Articles

[PageSpeed] 10

Debugging with Fiddler4, it appears that the Content-Type of the XML part is NOT set to application/atom+xml but to text/plain like the access_token which is added automatically to the body by the REST framework.
Note that the Content-Type at the top level is set to multipart/form-data by the REST framework.
This may be the issue but not sure how to correct this yet. Also note the =3D string being present in the XML part which I guess is replacing the = char during encoding.
 
----------052315110120278
Content-Disposition: form-data; name="{ABE6AA81-0A51-4176-A2CE-9C77AF5528D3}"
Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable

<?xml version=3D"1.0" encoding=3D"utf-8"?><entry xmlns:gd=3D"http://sc=
hemas.google.com/g/2005"><category scheme=3D"http://schemas.google.com=
/g/2005#kind" term=3D"http://schemas.google.com/contact/2008#group" />=
<title type=3D"text">Foo</title></entry>
----------052315110120278
Content-Disposition: form-data; name="access_token"
Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable

ya29.fAFVl6nSHxEH3TWA9384A1jF49j5AxeJT5...
----------052315110120278--
0
Jean
5/23/2015 12:24:48 PM
May it be that this is an issue with Delphi XE5 which would have been fixed in a later release?
This post (http://stackoverflow.com/questions/24305816/) is outlining some XE5 and XE6 difference in results.
I browsed QualityCentral but didn't find any specific ticket outlining a change/fix in REST framework which could explain this.
0
Jean
5/26/2015 8:13:05 AM
Jean-Fabien Connault <> wrote in news:724824@forums.embarcadero.com:

> May it be that this is an issue with Delphi XE5 which would have been
> fixed in a later release? This post
> (http://stackoverflow.com/questions/24305816/) is outlining some XE5
> and XE6 difference in results. I browsed QualityCentral but didn't
> find any specific ticket outlining a change/fix in REST framework
> which could explain this. 
> 

The issue is a windows (win32) error code that is returned from a win32 
call.
0
Christopher
5/26/2015 1:01:58 PM
> The issue is a windows (win32) error code that is returned from a win32 
> call.

Thanks. Do you have specific/further details? Is there a workaround?
0
Jean
5/26/2015 9:39:13 PM
Jean-Fabien Connault <> wrote in news:724905@forums.embarcadero.com:

>> The issue is a windows (win32) error code that is returned from a win32 
>> call.
> 
> Thanks. Do you have specific/further details? Is there a workaround?
> 

Specific details given in original post, no work around in general.

Luckily the API vendor I'm talking to support a non standard Authorization 
header which doesn't get BASE64 encoded, and that seems to work (by 
manually adding a header).

But the Authorization mechanism built into Delphi's REST/JSON seems to fail 
over 56 character long usernames.
0
Christopher
5/27/2015 12:18:41 PM
Here's a working workaround:

RESTClient.BaseURL :=  'https://www.google.com/m8/feeds';
//RESTClient.Authenticator := OAuth2_GoogleContacts;
RESTClient.ContentType := 'application/atom+xml';

RESTRequest.Method := TRESTRequestMethod.rmPOST;
RESTRequest.Resource := 'groups/default/full';

RESTRequest.Accept := 'application/atom+xml';
RESTRequest.Params.AddHeader('GData-Version', '3.0');

XMLText := '<?xml version="1.0" encoding="utf-8"?>' +
'<entry xmlns:gd="http://schemas.google.com/g/2005">' +
'<category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#group" />' +
'<title type="text">Foo</title>' +
'</entry>';

RESTRequest.Params.AddItem('Authorization', 'Bearer ' + OAuth2_GoogleContacts.AccessToken, TRESTRequestParameterKind.pkHTTPHEADER, [poDoNotEncode]);
RESTRequest.AddBody(XMLText, ctAPPLICATION_ATOM_XML);

RESTRequest.Execute;

The key is to NOT set the Authenticator (commented in the above code) and to add manually the AccessToken as an Authorization header.
0
Jean
5/30/2015 1:06:52 PM
Not REST related but needed for proper UTF-8 encoding.

This line...

RESTRequest.AddBody(XMLText, ctAPPLICATION_ATOM_XML);

....should be replaced with...

RequestStringStream := TStringStream.Create(XMLText, TEncoding.UTF8);
RESTRequest.AddBody(RequestStringStream, TRESTContentType.ctAPPLICATION_ATOM_XML);
RequestStringStream.Free;
0
Jean
6/4/2015 9:08:02 AM
Reply:

Similar Artilces:

ERROR >>>>>>>>>>>master page
can any budy give me an answer of given question this question is ask in interview we have an usercontrol in .net 1.0,1.1 why microsoft implimented the masterpage concept in 2.0 as work done by master page is same as usercontrol do   can any budy gives mw an ans -----------why we use master pages........???????? Friend, this article explains you why do we use the master pages http://dotnetperls.com/Content/Master-Page-Code-Behind.aspx and this one http://www.careerride.com/ASP.NET-Master-Pages-Questions.aspx Good luck!Please Don't forget to click "Mark as Answer&quo...

>>>> make test had returned bad status, won't install without force
Hello, I get an error while trying to install the bugzilla packages: /usr/bin/perl -MCPAN -e 'install "Email::MIME"' =20 =20 Bugzilla version: bugzilla-3.0.5 Perl Version: v5.8.5 OS : REDHAT 2.6.9-22.EL=20 =20 =20 =20 The problem: --------------------- =20 Result: FAIL Failed 4/6 test programs. 1/2 subtests failed. make: *** [test_dynamic] Error 255 /usr/bin/make test -- NOT OK Running make install make test had returned bad status, won't install without force =20 Regards Aneesh =20 ...

localhost error please help me>>>>
hi to all i want create a new project in visual studio dot ner. but im unable to create the error message is The web server reported the following error when attempting to create or open the web project located at the following URL: "http//localhost/webApplication1". '' please tell me what to do ? Are you using visual studio 2003 or 2005? Hemant Purkar( Software Consultant )-------------------------------------------------Please mark it as answer if it helps you resolve Could you tell us what the error was? It should be the bit right after what you've posted above. Thanks,&n...

Firefox >> MySpace >> Error Message
This is a multi-part message in MIME format. ------=_NextPart_000_0006_01C81C07.5CAB5420 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable I'm new to Firefox and now remember why I uninstalled it once before. When I try to access MySpace accounts with the 'music jukebox', I get a = dialog box that asks if I want to add this song to my profile. = Regardless of which button I press, I cannot my band = (www.myspace.com/djangotwango) or any other that uses the player. It = displays a error message, " An unexpected...

POST a request to Google Calendar/v3 API returns a parse error
Hello, I'm trying to send HTTP requests to the Google Calendar/v3 API using the REST client library components in DELPHI XE5 Update2(TRESTClient,TRESTRequest,TRESToAuth2Autenticator). After settings this components in Delphi EDI, GET requests is working well, returning desired JSON response. But POST request (https://www.googleapis.com/calendar/v3/calendars/primary/events) with a parameter in TRESTRequest.params setting up as pkREQUESTBODY with a value of {-"end": {"date": "2014-06-13"},-"start": {"date": "2014-06-13"},"summary": "reTest"} return a Google API error: {"error":{"errors":[{"domain":"global","reason":"parseError","message":"This API does not support parsing form-encoded input."}],"code":400,"message":"This API does not support parsing form-encoded input."}} Is there someting I'm doing wrong , how can I do to get the request working ? Thanks for your help. ...

HELP>>HELP>>>
The server abended due to a room that was too hot, it corrupted GroupWise so now when it reboots it hangs. How do I restart the server and get it not to load the autoexec.ncf which contains GW? Your immediate assistance would be greatly appreciated! Thanks. Barbara barbara.roy@po.state.ct.us wrote: > The server abended due to a room that was too hot, it corrupted GroupWise > so now when it reboots it hangs. How do I restart the server and get it > not to load the autoexec.ncf which contains GW? Your immediate assistance > would be greatly appreciated! Thanks. B...

applying signatures>>>>>
Name: Greg Donsberger Email: donsbergeratecnodotcom Product: Thunderbird Summary: applying signatures>>>>> Comments: Thunderbird!!! I'd like to be able to apply a signature to my e-mails......but not every one with a particular account. outlook express has an "insert option" for signatures....you still have to set it up.....but you insert it yourself, instead of it being a default setting for the account. Browser Details: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3 (.NET CLR 3.5.30729) From ...

diff between $request->content($c) and $ua->post($url, {}, Content => $c)
Hi, I'm wondering what the difference is between providing content to the HTTP::Request instance: my $req = HTTP::Request->new(POST => $self->url); $req->content($c); and creating a LWP::UserAgent instance and using the Content pseudo header. I have a more practical question next. On Wed, May 27, 2009 at 16:29, Terrence Brannon <metaperl@gmail.com> wrote: > Hi, > > I'm wondering what the difference is between providing content to the > HTTP::Request instance: > > =C2=A0my $req =3D HTTP::Request->new(POST =3D> $self-...

Fox 3 Beta 5 >>>>>>>
Name: Hank Hill Product: Firefox Summary: Fox 3 Beta 5 >>>>>>> Comments: Wooosh! Love what it does. Faster than anything. I look forward to more. Thank you. From URL: http://hendrix.mozilla.org/ ...

simple code for search please>>>>>>>
hi>>i am work in project with asp.neti have code that search words in  only one page ,but i want simple code for search words in multiple pagesi mean i want put search box in home page and i can search for words in all pagesplease help me and dont late...');" title="Sad - ">......thanks   can u give us more information about how ur information is stored in your website so we can help u decide which search mechanism to use? cuz search can be done in a million ways  If this post was useful to you, please mark it as correct answer. Thank yo...

Error
I am invoking a web method from a proxy stub and getting the following error. I can't seem to find any information on this particular error, anyone seen this before? Server was unable to process request. --> SoapException --> The requested address is not valid in its context...

New Download>>>>> VERY SLOW
Name: john Email: jsuggatcomcastdotnet Product: Firefox Summary: New Download>>>>> VERY SLOW Comments: when i got on the web this morning with firefox 3.0.6 there was a message that a firefox upgrade had downloaded. now, web pages load VERY, VERY, VERY SLOW. ABOUT 10 TIMES AS LONG TO LOAD, MAYBE MORE. help!! Browser Details: Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10.4; en-US; rv:1.9.0.6) Gecko/2009011912 Firefox/3.0.6 From URL: http://hendrix.mozilla.org/ Note to readers: Hendrix gives no expectation of a response to this feedback but if you wish to provide one you must BCC (not CC) the sender for them to see it. ...

next >> last>>
Hii, I have a datalist that shows students' names from a table. There are 100 names. I don't want to show the whole 100 names at once, instead i want to show only 20 names in a page, then i can see the next 20... and so on.. How can i do that? Can anyont help me? Thanks in advance KOUBBAZ 4 Life!! FYI: http://authors.aspalliance.com/das/datalistpaging.aspx Colt...

Sql Server 2000->->->->->Data->->->-> Sql Server 2005
How can i migrate data from Sql Server 2000 to  Sql Server 2000 A backup file of SQL 2000 can be restored on SQL 2005   ThanksMark post(s) as "Answer" that helped youElectronic ScrewWebsite||Blog||Dub@i.net...

PHP -> JSON ->Zlib -> Delphi XE7 [win32 -> work | android -> No mapping ] [Edit]
Мy program for android should get data from the server as yson when to perform under vindovs works, and in android error, please help! PHP: {code} $err = array(); $data = array('id'=>1,'name'=>'test','info'=>'1234'); $inf = array('data' => $data ,'error' => $err); $json = json_encode($inf); $json = gzencode(trim(preg_replace('/\s+/',' ',$json )),9); header('Content-Encoding: gzip'); header("content-type: text/html; charset: UTF-8"); header("cache-control: must-revalidate&qu...

SOS. >>>>>>>>HELP ME????????>>Web Matrix do not work in Win XP pro
Hi everbody!!!! I have a problem with webmatrix. My problem is?? i install ASP.NET Web Matrix but it is not work. My system is XP pro. I alredy install NET Framework 1.1. The program do not show nothing. But when i open i see picture which there webmatrix after some this disappeared and after i wait but this not work. When i use win 2000 this work excellent. But now problem . I test this in two PC. But no result. I want also add that i install XP sp 2. But Proqram do not work... Thanks!!!! Hi, Can you take a look at these threads: view post 258398 and view post 252245 Regar...

PHP -> JSON ->Zlib -> Delphi XE7 [win32 -> work | android -> No mapping ] [Edit] #2
Мy program for android should get data from the server as yson when to perform under vindovs works, and in android error, please help! PHP: {code} $err = array(); $data = array('id'=>1,'name'=>'test','info'=>'1234'); $inf = array('data' => $data ,'error' => $err); $json = json_encode($inf); $json = gzencode(trim(preg_replace('/\s+/',' ',$json )),9); header('Content-Encoding: gzip'); header("content-type: text/html; charset: UTF-8"); header("cache-control: must-revalidate&qu...

PHP -> JSON ->Zlib -> Delphi XE7 [win32 -> work | android -> No mapping ] [Edit] #2
Мy program for android should get data from the server as json when to perform under Win32 works, and in android error, please help! PHP: {code} $err = array(); $data = array('id'=>1,'name'=>'test','info'=>'1234'); $inf = array('data' => $data ,'error' => $err); $json = json_encode($inf); $json = gzencode(trim(preg_replace('/\s+/',' ',$json )),9); header('Content-Encoding: gzip'); header("content-type: text/html; charset: UTF-8"); header("cache-control: must-revalidate"...

PHP -> JSON ->Zlib -> Delphi XE7 [win32 -> work | android -> No mapping ] [Edit] #2
Мy program for android should get data from the server as yson when to perform under vindovs works, and in android error, please help! PHP: {code} $err = array(); $data = array('id'=>1,'name'=>'test','info'=>'1234'); $inf = array('data' => $data ,'error' => $err); $json = json_encode($inf); $json = gzencode(trim(preg_replace('/\s+/',' ',$json )),9); header('Content-Encoding: gzip'); header("content-type: text/html; charset: UTF-8"); header("cache-control: must-revalidate&qu...

Error in Admin >>> Site Settings
Hi, Could anyone help me with an error which appears twice in the Admin >>> Site Settings tab of my dotnetnuke 3.2.2 installation (which I recently upgraded). The error appears as: An error has occurred.DotNetNuke.Services.Exceptions.ModuleLoadException: Folder ---> System.IndexOutOfRangeException: Folder at System.Data.Common.FieldNameLookup.GetOrdinal(String fieldName) at System.Data.SqlClient.SqlDataReader.GetOrdinal(String name) at System.Data.SqlClient.SqlDataReader.get_Item(String name) at DotNetNuke.Common.Globals.GetFileList(Int32 PortalId, String strExtensions, Boolean...

GWIA -> API -> Exchange: Bad Message
Hi, I got everything working between my Groupwise 6.5.5 and Exchange 2003 SP2 system (internal mail flow, synchronisation) except for inbound mail from the internet. Messages that are sent using the GWIA to a user in the foreign system, do end up in the API queues, are collected by the Groupwise connector in Exchange, but then are discarded by Exchange as beeing Bad messages. The events (ignore the dates): ===================================================== Event Type: Information Event Source: MSExchangeGWRtr Event Category: General Event ID: 6001 Date: 4/29/2004 Ti...

Problem with the sqldatasource while bindign to a Dropdown list >>>>>>?
Hi I have a Gridview which has say 2 records with 2 columns -----------------------------------------------------                   Name            Age Select         xyvcvc            24 select          yvchj            Null -------------...

Error message >>>can not use a leading
I get an error message , but I have a hard time fixing it   anybody have an idea? Event code: 3005 Event message: An unhandled exception has occurred. Event time: 29.06.2007 07:25:01 Event time (UTC): 29.06.2007 04:25:01 Event ID: 046360bddd314a378a903c1151dd52f8 Event sequence: 8732 Event occurrence: 19 Event detail code: 0   Application information: Application domain: /LM/W3SVC/2043324371/Root-2-128275193396156250 Trust level: Full Application Virtual Path: / Application Path: C:\Inetpub\wwwroot\msnbc Machine name: MSNBC   Process information...

customErrors mode >> Error > Please help
Hi I upload my ngallery to my webmatrix host but after browsing the default page i saw this error ===================== Runtime Error Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine. Details: To enable the details of this specific error message to be viewable on remote machines, please create a <customErrors> tag within a "w...

Web resources about - REST>Google Contacts API>POST returns a bad request error (400) - embarcadero.delphi.webservices

Resources last updated: 3/16/2016 8:48:19 AM