我正在使用Google Contact API实施Contact应用程序. 现在,我尝试通过发送以下格式的放置请求来更新联系人
I'm implementing a Contact application using Google Contact API. Now I'm trying to update a contact by sending a put request in the below format
PUT /m8/feeds/contacts/default/full/{contactId} If-Match: {lastKnownEtag} GData-Version: 3.0 Content-Type: application/atom+xml我将XML作为字符串发送给请求主体. 这是我的xmlString(请求正文)
And I have the XML as a string that I'm going to send as a body of the request. This is my xmlString (Body of the Request)
<?xml version="1.0" encoding="UTF-8"?> <entry xmlns:gd="schemas.google/g/2005" gd:etag="*"> <id>www.google/m8/feeds/contacts/default/base/1785xxxx</id> <catagory scheme="schemas.google/g/2005#kind" term="schemas.google/contact/2008#contact"/> <gd:name> <gd:fullname>abc</gd:fullname></gd:name> <gd:email address="abc@gmail" displayName="abc" primary="true" rel="schemas.google/g/2005#work"/> <content type="text">Notes</content> <gd:phoneNumber primary="true" rel="schemas.google/g/2005#other">9090xxxxxx</gd:phoneNumber> </entry>我已经编写了以下代码来发送PUT请求以更新联系人.
I have written the below code to send a PUT request to Update a Contact .
String getUrl = "www.google/m8/feeds/contacts/default/full/"+contactID+"?oauth_token=" + accessToken; URL url = new URL(getUrl); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setDoOutput(true); con.setRequestMethod("PUT"); con.setRequestProperty("Content-Type", "application/atom+xml" ); con.setRequestProperty("GData-Version","3.0"); con.setRequestProperty("IF-MATCH", "*"); OutputStreamWriter output = new OutputStreamWriter(con.getOutputStream()); output.write(xmlString); // xmlString is the body of the request output.flush(); output.close(); System.out.println(con.getResponseCode());当我尝试在OAuth 2.0 Playground中发送请求时,联系人已成功更新. 但是,当我尝试运行上述程序时,我得到了
When I tried to send the request in OAuth 2.0 Playground , the contact is updated successfully. But when I try to run the above program I'm getting
400错误的请求错误
400 Bad Request Error
我不知道我要去哪里.任何帮助将不胜感激!
I don't know where I'm going wrong. Any help would be appreciated!
推荐答案最后,我找到了我要去的地方.
Finally I have found where I'm going wrong.
我的xmlString无效. <entry>标记需要另一个名称空间xmlns="www.w3/2005/Atom",在 developers.google中未提及/contacts/v3 ( Google Contact API ). 这就是为什么我收到400错误的请求错误的原因.
My xmlString invalid. <entry> tag requires another namespace xmlns="www.w3/2005/Atom" which is not mentioned in developers.google/contacts/v3 (Google Contact API). That's the reason why I got 400 Bad request error.
有效xmlString
Valid xmlString
<?xml version="1.0" encoding="UTF-8"?> <entry xmlns:gd="schemas.google/g/2005" gd:etag="*" xmlns="www.w3/2005/Atom"> <id>www.google/m8/feeds/contacts/default/base/1785xxxx </id> <category scheme="schemas.google/g/2005#kind" term="schemas.google/contact/2008#contact" /> <gd:name> <gd:fullName>name</gd:fullName> </gd:name> <gd:email address="juli@gmail" displayName="juli" primary="true" rel="schemas.google/g/2005#work" /> <content type="text">Notes</content> <gd:phoneNumber primary="true" rel="schemas.google/g/2005#other">9090xxxxxx</gd:phoneNumber> </entry>更多推荐
PUT请求给出400错误请求错误
发布评论