繁体   English   中英

PUT请求给出400错误请求错误

[英]PUT request giving 400 Bad Request Error

我正在使用Google Contact API实施Contact应用程序。 现在,我尝试通过发送以下格式的放置请求来更新联系人

PUT /m8/feeds/contacts/default/full/{contactId}
If-Match: {lastKnownEtag}
GData-Version: 3.0
Content-Type: application/atom+xml

我将XML作为字符串发送给请求主体。 这是我的xmlString( 请求的正文)

<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns:gd="http://schemas.google.com/g/2005" gd:etag="*">
<id>http://www.google.com/m8/feeds/contacts/default/base/1785xxxx</id>
<catagory scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#contact"/>
<gd:name>
<gd:fullname>abc</gd:fullname></gd:name>
<gd:email address="abc@gmail.com" displayName="abc" primary="true" rel="http://schemas.google.com/g/2005#work"/>
<content type="text">Notes</content>
<gd:phoneNumber primary="true" rel="http://schemas.google.com/g/2005#other">9090xxxxxx</gd:phoneNumber>
</entry>

我已经编写了以下代码来发送PUT请求以更新联系人。

    String getUrl = "https://www.google.com/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发送请求时,联系人已成功更新。 但是当我尝试运行上面的程序时

400错误的请求错误

我不知道我要去哪里错了。 任何帮助,将不胜感激!

终于,我找到了我要去的地方。

我的xmlString无效。 <entry>标签需要另一个命名空间xmlns="http://www.w3.org/2005/Atom"未提及https://developers.google.com/contacts/v3谷歌联系API)。 这就是为什么我收到400错误的请求错误的原因。

有效的xmlString

<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns:gd="http://schemas.google.com/g/2005" gd:etag="*" xmlns="http://www.w3.org/2005/Atom">
    <id>http://www.google.com/m8/feeds/contacts/default/base/1785xxxx </id>
    <category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#contact" />
    <gd:name>
        <gd:fullName>name</gd:fullName>
    </gd:name>
    <gd:email address="juli@gmail.com" displayName="juli" primary="true" rel="http://schemas.google.com/g/2005#work" />
    <content type="text">Notes</content>
    <gd:phoneNumber primary="true" rel="http://schemas.google.com/g/2005#other">9090xxxxxx</gd:phoneNumber>
</entry>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM