400 : Invalid entry ID/URI 批量操作错误
400 : Invalid entry ID/URI error in Batch Operation
我目前正在 Java 中使用 Google 联系人 API 实施联系人应用程序。
并且我完成了联系人的个人增删改查。
现在我正在对联系人进行批量操作。
我已经创建了 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 Playground
和 Postman
中,我收到了有效回复。
但是当我在我的 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 替换为 full
但我遇到了同样的错误。有什么建议吗?
我修改了请求 XML 并最终使我的代码工作。
我用 userEmail
(我的邮件 ID)替换了 XML 中的每个 'default'
。
我仍然不知道为什么代码会产生 'default'
错误。我知道
The special userEmail value default can be used to refer to the authenticated user
但个别添加、删除和更新操作与 default
值完美配合,批量操作无法与默认值配合使用。
我目前正在 Java 中使用 Google 联系人 API 实施联系人应用程序。 并且我完成了联系人的个人增删改查。 现在我正在对联系人进行批量操作。
我已经创建了 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 Playground
和 Postman
中,我收到了有效回复。
但是当我在我的 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 替换为 full但我遇到了同样的错误。有什么建议吗?
我修改了请求 XML 并最终使我的代码工作。
我用 userEmail
(我的邮件 ID)替换了 XML 中的每个 'default'
。
我仍然不知道为什么代码会产生 'default'
错误。我知道
The special userEmail value default can be used to refer to the authenticated user
但个别添加、删除和更新操作与 default
值完美配合,批量操作无法与默认值配合使用。