gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:06:09 26 4
gpt4 key购买 nike

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

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

最佳答案

我有同样的错误,问题是 token 已过期。您可以使用 fiddler 确认这一点。我遇到了 401 错误。刷新 token 后,一切正常。希望有所帮助。

关于c# - ContactsRequest.Insert(feedUri, newEntry) 有时会因 System.Net.ProtocolViolationException 而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23804960/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com