400 Bad Request

38 views
Skip to first unread message

Rod Alfaro

unread,
May 24, 2012, 2:45:45 PM5/24/12
to google-co...@googlegroups.com
Hello,

Im trying to generate the post request to create a new shared contact.

this is my code 

      Mdata = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><atom:entry xmlns:atom='http://www.w3.org/2005/Atom'"
      Mdata += "xmlns:gd='http://schemas.google.com/g/2005'>"
      Mdata += "<atom:category scheme='http://schemas.google.com/g/2005#kind'"
      Mdata += "<gd:name>"
      Mdata += "<gd:givenName>Elizabeth</gd:givenName>"
      Mdata += "<gd:familyName>Bennet</gd:familyName>"
      Mdata += "<gd:fullName>Elizabeth Bennet</gd:fullName>"
      Mdata += "</gd:name>"
      Mdata += "<atom:content type='text'>Notes</atom:content>"
      Mdata += "<gd:email rel='http://schemas.google.com/g/2005#work'"
      Mdata += "primary='true'"
      Mdata += "address='l...@gmail.com' displayName='E. Bennet' />"
      Mdata += "<gd:email rel='http://schemas.google.com/g/2005#home'"
      Mdata += "address='l...@example.org' />"
      Mdata += "<gd:phoneNumber rel='http://schemas.google.com/g/2005#work'"
      Mdata += "primary='true'>"
      Mdata += "(206)555-1212"
      Mdata += "</gd:phoneNumber>"
      Mdata += "<gd:phoneNumber rel='http://schemas.google.com/g/2005#home'>"
      Mdata += "(206)555-1213"
      Mdata += "</gd:phoneNumber>"
      Mdata += "<gd:im address='l...@gmail.com'"
      Mdata += "protocol='http://schemas.google.com/g/2005#GOOGLE_TALK'"
      Mdata += "primary='true'"
      Mdata += "rel='http://schemas.google.com/g/2005#home' />"
      Mdata += "<gd:structuredPostalAddress"
      Mdata += "rel='http://schemas.google.com/g/2005#work'"
      Mdata += "primary='true'>"
      Mdata += "<gd:city>Mountain View</gd:city>"
      Mdata += "<gd:street>1600 Amphitheatre Pkwy</gd:street>"
      Mdata += "<gd:region>CA</gd:region>"
      Mdata += "<gd:postcode>94043</gd:postcode>"
      Mdata += "<gd:country>United States</gd:country>"
      Mdata += "<gd:formattedAddress>"
      Mdata += "1600 Amphitheatre Pkwy Mountain View"
      Mdata += "</gd:formattedAddress>"
      Mdata += "</gd:structuredPostalAddress>"
      Mdata += "</atom:entry>"
      
      lMdata = (Mdata)

      request = urllib2.Request('https://www.google.com/m8/feeds/contacts/' + self.domain + '/full', data=lMdata)      
      request.add_header('GData-Version', '3.0')
      request.add_header('Content-Type', 'application/atom+xml')
      request.add_header('Authorization', 'GoogleLogin auth=' + self.login_token)

      opener = urllib2.build_opener()      
      result = opener.open(request).read()

im sending the same sample data given in the api documentation. i make the request after a successfull login and providing the token in the header.

I get 

  File "C:\Python27\lib\urllib2.py", line 400, in open
    response = meth(req, response)
  File "C:\Python27\lib\urllib2.py", line 513, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python27\lib\urllib2.py", line 438, in error
    return self._call_chain(*args)
  File "C:\Python27\lib\urllib2.py", line 372, in _call_chain
    result = func(*args)
  File "C:\Python27\lib\urllib2.py", line 521, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 400: Bad Request

maybe the issue is sending xml data as body in a post request. can anyone there provide me sample of how to post xml to google apps api please.

Thanks for your help
Reply all
Reply to author
Forward
0 new messages