400:批处理操作中的无效条目ID / URI错误

时间:2018-06-26 04:57:50

标签: java xml google-contacts googlecontactsapi

我目前正在使用Java中的Google Contact API实施Contact Application。 我已经完成了联系人的单独添加,删除和更新。 现在,我正在对联系人实施批处理操作。

我已经创建了XML文件,该文件需要作为批处理操作的请求主体发送。以下是我创建的请求的正文

<?xml version="1.0" encoding="UTF-8"?>
<feed
    xmlns="http://www.w3.org/2005/Atom"
    xmlns:batch="http://schemas.google.com/gdata/batch"
    xmlns:gContact="http://schemas.google.com/contact/2008"
    xmlns:gd="http://schemas.google.com/g/2005">
    <entry gd:etag="*">
        <batch:id>delete</batch:id>
        <batch:operation type="delete"/>
        <id>https://www.google.com/m8/feeds/contacts/default/full/27f2a9228b8abcde</id>
    </entry>
    <entry gd:etag="*">
        <batch:id>update</batch:id>
        <batch:operation type="update"/>
        <id>www.google.com/m8/feeds/contacts/default/full/1f3f94f58cabcde</id>
        <category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/g/2005#contact"/>
        <gd:name>
            <gd:givenName>abc</gd:givenName>
        </gd:name>
        <content type="text">Notes</content>
        <gd:email address="klm@gmail.com" primary="true" rel="http://schemas.google.com/g/2005#home"/>
        <link href="https://www.google.com/m8/feeds/photos/media/default/1f3f94f58c4abcde" rel="http://schemas.google.com/contacts/2008/rel#photo" type="image/*"/>
        <link href="https://www.google.com/m8/feeds/contacts/default/full/1f3f94f58c4abcde" rel="self" type="application/atom+xml"/>
        <link href="https://www.google.com/m8/feeds/contacts/default/full/1f3f94f58cabcde" rel="edit" type="application/atom+xml"/>
        <gd:phoneNumber primary="true" rel="http://schemas.google.com/g/2005#other">912377xxxx</gd:phoneNumber>
    </entry>
    <entry>
        <batch:id>create</batch:id>
        <batch:operation type="insert"/>
        <category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/g/2008#contact"/>
        <gd:name>
            <gd:givenName>xyz</gd:givenName>
        </gd:name>
        <gd:email address="xyz@gmail.com" primary="true" rel="http://schemas.google.com/g/2005#home"/>
        <gd:phoneNumber primary="true" rel="http://schemas.google.com/g/2005#work">9876xxxxxx</gd:phoneNumber>
    </entry>
</feed>

当我将上述XML作为 POST 请求的正文发送到URI

https://www.google.com/m8/feeds/contacts/default/full/batch

OAuth PlaygroundPostman中,我得到了有效的答复。

但是当我在Java代码中将上述XML作为请求的主体发送时,我得到以下XML作为响应,并获得200作为响应代码。

    <?xml version='1.0' encoding='UTF-8'?>
<feed
    xmlns='http://www.w3.org/2005/Atom'
    xmlns:openSearch='http://a9.com/-/spec/opensearch/1.1/'
    xmlns:batch='http://schemas.google.com/gdata/batch'
    xmlns:gd='http://schemas.google.com/g/2005'
    xmlns:gContact='http://schemas.google.com/contact/2008'>
    <id>https://www.google.com/m8/feeds/contacts/testcontact105%40gmail.com/base/batch/1529948621576</id>
    <entry>
        <id>https://www.google.com/m8/feeds/contacts/default/base/27f2a9228b829055</id>
        <updated>2018-06-25T17:43:41.576Z</updated>
        <title>Error</title>
        <content>Invalid entry Id/Uri</content>
        <batch:id>delete</batch:id>
        <batch:status code='400' reason='Invalid entry Id/Uri'/>
        <batch:operation type='delete'/>
    </entry>
    <entry>
        <id>www.google.com/m8/feeds/contacts/default/base/1f3f94f58c410013</id>
        <updated>2018-06-25T17:43:41.577Z</updated>
        <title>Error</title>
        <content>Invalid entry Id/Uri</content>
        <batch:id>update</batch:id>
        <batch:status code='400' reason='Invalid entry Id/Uri'/>
        <batch:operation type='update'/>
    </entry>
    <entry>
        <id>www.google.com/m8/feeds/contacts/default/base/5054af850dde6483</id>
        <updated>2018-06-25T17:43:41.577Z</updated>
    <gd:name>    <gd:givenName>xyz</gd:givenName>     </gd:name>
    <batch:id>create</batch:id>
    <batch:status code='201' reason='Created'/>
    <batch:operation type='insert'/>
    <gd:email rel='http://schemas.google.com/g/2005#home' address='xyz@gmail.com' primary='true'/>
    <gd:phoneNumber rel='http://schemas.google.com/g/2005#work' uri='tel:+91-98679-99999' primary='true'>9867xxxxxx</gd:phoneNumber>
</entry>
</feed>

下面是我的Java代码

String getUrl = "https://www.google.com/m8/feeds/contacts/default/full/batch?oauth_token=" + accessToken;
URL url = new URL(getUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoOutput(true);
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/atom+xml");
con.setRequestProperty("Gdata-version","3.0");      
con.setRequestProperty("Content-Length", Integer.toString(responseXML.length()));
//responseXML is body of the request
con.getOutputStream().write(responseXML.getBytes("UTF8"));
System.out.println(con.getResponseCode() + ":" + con.getResponseMessage());

批处理插入操作正常运行。但是更新和删除操作无法正常工作。

我尝试用https://issuetracker.google.com/issues/36756279

中的建议将uri中的base完全替换为

但是我遇到了同样的错误。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我修改了请求XML,最后使我的代码正常工作。

我用'default'(我的邮件ID)替换了XML中的每个userEmail

我仍然不知道为什么代码会产生'default'错误。我知道

The special userEmail value default can be used to refer to the authenticated user

但是单独的添加,删除和更新操作可以与default值完美配合,而批处理操作不能与默认值一起使用。