繁体   English   中英

在Rails中使用oauth2创建Google联系人

[英]Create a google contacts using oauth2 in rails

最近几天,我一直在抓这个问题,所以我希望有人可以帮助我,因为我不知道在哪里挖。 如标题所述,我正在使用omniauth google oauth2进行身份验证的项目。 从一开始就可以正常工作。 下一部分是在Google通讯录api中创建通讯录,它不起作用。

由于要生成的xml / atom有点沉重,因此我决定使用xml视图并使用render_to_string在我的控制器中检索它。 因为我对Rails不太了解,所以我只是想让它像这样更干净。 这很好。

所以这是我的oauth2呼叫:

strxml = render_to_string( :partial => 'createInGoogle', :layout => false )
response = access_token.post('/m8/feeds/contacts/default/full', params: {:body => strxml, :header => 'Content-type: application/atom+xml;GData-Version: 3.0'})

当我调试该呼叫时,我可以看到生成的网址看起来像

"https://www.google.com/m8/feeds/contacts/default/full?body=%3Catom%3Aentry+xmlns%3Aatom%3D%27http%3A%2F%2Fwww.w3.org%2F2005%2FAtom%27+xmlns%3Agd%3D%27http%3A%2F%2Fschemas.google.com%2Fg%2F2005%27%3E%0A%3Catom%3Acategory+scheme%3D%27http%3A%2F%2Fschemas.google.com%2Fg%2F2005%23kind%27+term%3D%27http%3A%2F%2Fschemas.google.com%2Fcontact%2F2008%23contact%27%2F%3E%0A%3Cgd%3Aname%3E%0A%3Cgd%3AgivenName%3EPhilippe+Henri+Raymond%3C%2Fgd%3AgivenName%3E%0A%3Cgd%3AfamilyName%3EMichel+%3C%2Fgd%3AfamilyName%3E%0A%3Cgd%3AfullName%3EPhilippe+Henri+Raymond+Michel+%3C%2Fgd%3AfullName%3E%0A%3C%2Fgd%3Aname%3E%0A%3Catom%3Acontent+type%3D%27text%27%3ENotes%3C%2Fatom%3Acontent%3E%0A%3Cgd%3Aemail+rel%3D%27http%3A%2F%2Fschemas.google.com%2Fg%2F2005%23work%27+primary%3D%27true%27+address%3D%27fifoooo%40gmail.com%27+displayName%3D%27Philippe+Henri+Raymond+Michel+%27%2F%3E%0A+++++++++++++++++++++++++++++++++++++%C3%A9%0A%3Cgd%3AphoneNumber+rel%3D%27http%3A%2F%2Fschemas.google.com%2Fg%2F2005%23work%27+primary%3D%27true%27%3E%0A04442453369%0A%3C%2Fgd%3AphoneNumber%3E%0A%3Cgd%3AphoneNumber+rel%3D%27http%3A%2F%2Fschemas.google.com%2Fg%2F2005%23work%27+primary%3D%27true%27%3E%0A%2B33662473369%0A%3C%2Fgd%3AphoneNumber%3E%0A%3Cgd%3AstructuredPostalAddress+rel%3D%27http%3A%2F%2Fschemas.google.com%2Fg%2F2005%23work%27+primary%3D%27true%27%3E%0A%3Cgd%3Acity%3ESausset+les+pins%3C%2Fgd%3Acity%3E%0A%3Cgd%3Astreet%3E9+Avenue+G%C3%A9n%C3%A9ral+de+Gaulle+%3C%2Fgd%3Astreet%3E%0A%3Cgd%3Apostcode%3E13960%3C%2Fgd%3Apostcode%3E%0A%3Cgd%3AformattedAddress%3E%0A9+Avenue+G%C3%A9n%C3%A9ral+de+Gaulle++13960+Sausset+les+pins%0A%3C%2Fgd%3AformattedAddress%3E%0A%3C%2Fgd%3AstructuredPostalAddress%3E%0A%3C%2Fatom%3Aentry%3E&header=Content-type%3A+application%2Fatom%2Bxml%3B+GData-Version%3A+3.0"

如您所见,关于gem如何格式化xml ...肯定存在问题。然后出现了在调试器中找到的响应

Content-Type application / x-www-form-urlencoded不是有效的输入类型。

所以我想我的xml格式有问题,或者我的内容类型有误

好的,这是正确的调用方式:

strxml = render_to_string( :partial => 'createInGoogle', :layout => false )
response = access_token.post('/m8/feeds/contacts/default/full', {:body => strxml, :header => {'Content-type' => 'application/atom+xml;GData-Version: 3.0'}})

暂无
暂无

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

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