ContactsRequest.Insert(feedUri,newEntry)有时会因System.Net.ProtocolViolationException而失败

问题描述 投票:2回答:1

我有这段代码用于在我创建的测试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 = "[email protected]"
        });


        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);
}

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

c# gdata google-contacts
1个回答
3
投票

我有同样的错误,问题是令牌已过期。您可以使用fiddler确认。我有401错误。一旦我刷新令牌,一切正常。希望有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.