400: неверный идентификатор записи / ошибка URI в пакетной операции
В настоящее время я внедряю приложение Contact, используя Google Contact API на Java . И я завершил индивидуальное добавление, удаление и обновление контактов. Сейчас я реализую пакетную операцию над контактами.
Я создал файл 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
Я получаю действительный ответ.
Но когда я отправляю приведенный выше XML как тело запроса в моем Java-коде, я получаю приведенный ниже 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());
Пакетная вставка работает отлично. Но операции обновления и удаления не работают должным образом.
Я попытался заменить базу на полную в URI предложением, найденным в https://issuetracker.google.com/issues/36756279
Но я получаю ту же ошибку. Какие-либо предложения?
1 ответ
Я изменил XML-запрос и, наконец, заставил мой код работать.
Я заменил каждый 'default'
в XML с userEmail
(Мой почтовый идентификатор).
Я до сих пор не знаю, почему код выдает ошибку с 'default'
, я знаю
The special userEmail value default can be used to refer to the authenticated user
Но отдельные операции добавления, удаления и обновления отлично работают с default
Значения и пакетные операции не работают со значением по умолчанию.