【问题标题】:ContactsRequest.Insert(feedUri, newEntry) sometimes fails with System.Net.ProtocolViolationExceptionContactsRequest.Insert(feedUri, newEntry) 有时会因 System.Net.ProtocolViolationException 而失败
【发布时间】:2014-05-22 11:00:31
【问题描述】:

我有这段代码用于在我创建的测试 gmail 帐户中添加联系人:

public class SomeClass
{
    private const string ClientId = "someclientid"
    private const string CliendSecret = "muchsecretwow";

    private const string ApplicationName = "such app";
    private const string RedirectUri = "http://localhost";

    private const string Scopes = "https://www.google.com/m8/feeds/";
    private OAuth2Parameters _parameters;

    private string _accessToken, _refreshToken;
    public void GoogleApiCallAddContact() {
        GetOAuthParameters();
        if (!GetTokensFromMemory())
            throw new Exception("please create new authorization code");
        _parameters.AccessToken = _accessToken;
        _parameters.RefreshToken = _refreshToken;

        var settings = new RequestSettings(ApplicationName, _parameters);


        var cr = new ContactsRequest(settings);

        var newEntry = new Contact {
            Name = new Name {
                FullName = "John Foo",
                GivenName = "John",
                FamilyName = "Foo",
            },
            Content = "some info"
        };

        newEntry.Emails.Add(new EMail {
            Primary = true,
            Rel = ContactsRelationships.IsOther,
            Address = "foo@somemailserver.com"
        });


        var feedUri = new Uri("https://www.google.com/m8/feeds/contacts/default/full");


        cr.Insert(feedUri, newEntry);



    }

    private void GetOAuthParameters() {
        _parameters = new OAuth2Parameters {
            ClientId = ClientId,
            ClientSecret = CliendSecret,
            RedirectUri = RedirectUri,
            Scope = Scopes,
        };
    }

    private bool GetTokensFromMemory() {
        if (File.Exists("./tokens.txt")) {
            var lines = File.ReadLines("./tokens.txt").ToList();
            _accessToken = lines[0];
            _refreshToken = lines[1];
            return true;
        }
        _accessToken = _refreshToken = null;
        return false;
    }
}

有时(有时不是,可能取决于非确定性参数)我得到这个异常:

System.Net.ProtocolViolationException : When performing a write operation with AllowWriteStreamBuffering set to false, you must either set ContentLength to a non-negative number or set SendChunked to true.
   at System.Net.HttpWebRequest.CheckProtocol(Boolean onRequestStream)
   at System.Net.HttpWebRequest.GetResponse()
   at Google.GData.Client.GDataRequest.Execute()
   at Google.GData.Client.GDataGAuthRequest.Execute(Int32 retryCounter)
   at Google.GData.Client.GOAuth2Request.Execute()
   at Google.GData.Client.Service.EntrySend(Uri feedUri, AtomBase baseEntry, GDataRequestType type, AsyncSendData data)
   at Google.GData.Client.Service.Insert(Uri feedUri, AtomEntry newEntry, AsyncSendData data)
   at Google.GData.Client.Service.Insert(Uri feedUri, TEntry entry)
   at Google.GData.Client.FeedRequest`1.Insert(Uri address, Y entry)
   at SomeDirectory.Tests.SomeClass.GoogleApiCallAddContact() in GmailApiLearningTests.cs: line 124

这似乎超出了我的代码范围,因为它深入 gdata 的实现。奇怪的是,当我在添加联系人时遇到此异常时,另一个使用 ContactRequest 获取所有联系人的测试工作得很好。对此有何见解?


更新:对于遇到相同问题的任何人,请执行以下操作:

try{
   cr.Insert(feedUri,newEntry);
}
catch(System.Net.ProtocolViolationException)
{
   cr.Insert(feedUri,newEntry);
}

问题是第一次插入失败(因为无效的访问令牌),客户端库调用 OAuthUtil.RefreshAccessToken(parameters) 但不知何故无法使用新令牌重新发出插入,或者至少失败并出现 GDataRequestException ->未经授权的WebException。因此,通过执行上述操作,您可以刷新令牌并手动重新发出插入调用。

【问题讨论】:

    标签: c# gdata google-contacts-api


    【解决方案1】:

    我遇到了同样的错误,问题是令牌已过期。您可以确认使用提琴手。我有一个 401 错误。刷新令牌后,一切正常。希望有所帮助。

    【讨论】:

    • 看来你是对的。尽管访问令牌会自动刷新(就像在所有请求中一样)很奇怪,但是 contactsRequest.Insert 会抛出,而不是使用像 GetContacts() 或 Retrieve 这样的新令牌。再次感谢。
    • When performing a write operation with AllowWriteStreamBuffering set to false, you must either set ContentLength to a non-negative number or set SendChunked to true.我遇到了这个错误,使用Contact createdEntry = cr.Insert(request.RequestUri, newEntry)这个代码创建联系人。请帮助我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-10
    • 1970-01-01
    • 1970-01-01
    • 2018-03-09
    • 1970-01-01
    • 2012-11-21
    • 2020-10-12
    相关资源
    最近更新 更多