【问题标题】:401 Unauthorized error while trying to create shared Google App Contact尝试创建共享的 Google App 联系人时出现 401 未经授权的错误
【发布时间】:2016-03-21 05:11:19
【问题描述】:

我正在尝试使用 GData .Net Api 创建一个新联系人。 我有一个使用较新的 Google.Apis 的 AccessToken。

此访问令牌可以正常获取、更新和删除联系人,但如果我尝试创建一个,我会收到 401 Unauthorized 响应。

这是我用来添加联系人的代码:

        if (string.IsNullOrEmpty(FullName))
        {
            FullName = string.Format("{0} {1}", FirstName, LastName);

            if (string.IsNullOrEmpty(FullName))
            {
                ThrowTerminatingError(new ErrorRecord(
                    new ArgumentException("Please provide a name for the contact"),
                    null, ErrorCategory.InvalidArgument, null));
            }
        }

        Contact = new Contact
        {
            Name = new Name
            {
                GivenName = FirstName,
                FamilyName = LastName,
                FullName = FullName
            },
            Content = "Notes",                    
        };

        foreach (var m in Emails)
        {
            Contact.Emails.Add(new EMail(m));
        }
        RequestSettings settings = new RequestSettings(applicationName, AuthentParameters);
        ContactsRequest cr = new ContactsRequest(settings);

        var feedUri = new Uri(string.Format("{0}{1}/full/", Scope, Domain));
        cr.Insert(feedUri, Contact));

以下原子提要使用 POST 方法发送到http://www.google.com/m8/feeds/contacts/(my domain)/full/:

<?xml version="1.0" encoding="utf-8"?>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gd="http://schemas.google.com/g/2005">
  <gd:name>
    <gd:givenName>Guillaume</gd:givenName>
    <gd:familyName>Davion</gd:familyName>
    <gd:fullName>Guillaume Davion</gd:fullName>
  </gd:name>
  <gd:email address="gudavion@test.info" />
  <category term="http://schemas.google.com/contact/2008#contact" scheme="http://schemas.google.com/g/2005#kind" />
  <content type="text">Notes</content>
</entry>

标题是:

  • 内容类型:应用程序/原子+xml; charset=UTF-8
  • 授权:承载 ya29.(访问令牌)
  • GData-版本:3.0

感谢任何可以帮助我的人。

【问题讨论】:

标签: google-api gdata google-contacts-api google-oauth


【解决方案1】:

我设法使它与两件事一起工作: 首先,改变我构建提要 uri 的方式:

var feedUri = new Uri(ContactsQuery.CreateContactsUri(Domain));

然后在电子邮件中添加标签:

Contact.Emails.Add(new EMail(m) { Label = "Default" });

这样,添加就顺利进行了。

【讨论】:

    猜你喜欢
    • 2011-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    • 2021-05-28
    • 2016-12-14
    相关资源
    最近更新 更多