4

cURL で新しい連絡先を作成できましたが、この連絡先にグループ メンバーシップを追加しようとすると、400 エラーが発生します。このドキュメントを読ん で同じリクエストをしましたが、うまくいきませんでした。私は何を間違っていますか?アイデアをありがとう!

これは、グループ情報を含む XML を作成する方法です。

$doc = new DOMDocument('1.0', 'UTF-8');
$doc->formatOutput = true;

$entry = $doc->createElement('entry');
$entry->setAttribute('gd:etag', $etag);
$doc->appendChild($entry);

$category = $doc->createElement('category');
$category->setAttribute('scheme', 'http://schemas.google.com/g/2005#kind');
$category->setAttribute('term', 'http://schemas.google.com/contact/2008#contact');
$entry->appendChild($category);

$id = $doc->createElement('id', 'http://www.google.com/m8/feeds/contacts/default/base/'.$idgmail);
$entry->appendChild($id);

$updated = $doc->createElement('updated', $update_info);
$entry->appendChild($updated);

// Add group info (My Contacts)

$group = $doc->createElement('gContact:groupMembershipInfo');
$entry->appendChild($group);
$group->setAttribute('deleted', 'false');
$group->setAttribute('href', 'http://www.google.com/m8/feeds/groups/default/base/6');

// Add another group info

$group = $doc->createElement('gContact:groupMembershipInfo');
$entry->appendChild($group);
$group->setAttribute('deleted', 'false');
$group->setAttribute('href', 'http://www.google.com/m8/feeds/groups/default/base/'.$my_group_id);

$group_info = $doc->saveXML();

そして、これは私のcURLです:

$url = 'https://www.google.com/m8/feeds/contacts/default/full/'.$idgmail.'/';

$headers = array(
'Host: www.google.com',
'Gdata-version: 3.0',
'Content-length: '.strlen($group_info),
'Content-type: application/atom+xml',
'If-Match: *',
'Authorization: OAuth '.$access,
);

$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $group_info);
curl_setopt($curl, CURLOPT_FAILONERROR, true);

$resp = curl_exec($curl); 
print_r($resp); // Prints nothing
echo curl_getinfo($curl, CURLINFO_HTTP_CODE); // Gives 400
curl_close($curl);
4

1 に答える 1

8

OK、私は自分でそれを理解しました:)
1) まず、連絡先を更新するには、POST ではなく PUT リクエストを使用する必要があります。
2) XML では "default" を使用できません (別のエラーが発生します)。完全な電子メール アドレスを使用する必要があります。

$group = $doc->createElement('gContact:groupMembershipInfo');
$entry->appendChild($group);
$group->setAttribute('deleted', 'false');
$group->setAttribute('href', 'http://www.google.com/m8/feeds/groups/user.email@gmail.com/base/6');  

3) 名前空間 gContact を指定していない場合、400 エラーが発生します。entry タグの全体は次のようになります。

$entry = $doc->createElement('entry');
$entry->setAttribute('xmlns', 'http://www.w3.org/2005/Atom');
$entry->setAttribute('xmlns:gd', 'http://schemas.google.com/g/2005');
$entry->setAttribute('xmlns:gContact', 'http://schemas.google.com/contact/2008');
$doc->appendChild($entry); 

4) 最後に、特定のグループに連絡先を追加するには、(ドキュメントから考えたように) 更新する必要はありません。連絡先の作成中に行うことができます (はい、今では明らかです)。最初に連絡先のグループを作成せずに更新しようとすると、400 エラーが発生します (エントリにフィールドが設定されていません)。
これが誰かを助けることを願っています!
PS これらの問題を解決するために、Google の「OAuth 2.0 Playground」を使用しました。非常に便利です。https://developers.google.com/oauthplayground/

于 2013-03-22T19:57:30.757 に答える