有人能帮我解决下面的例外吗?下面是用于将收件箱消息移动到另一个标签的.Net代码
{
List<String> labelsToAdd = new List<string>();
labelsToAdd.Add("Label_1");
//List<String>`enter code here` labelsToRemove = new List<string>();
//labelsToRemove.Add("INBOX");
ModifyMessageRequest mods = new ModifyMessageRequest();
mods.AddLabelIds = labelsToAdd;
//mods.RemoveLabelIds = labelsToRemove;
gmail.Users.Messages.Modify(mods, usr, email.Id).Execute();
}
我得到了这样的错误
GoogleAPIException -{“发生错误,但错误响应不能反序列化”} Newtonsoft.Json.JsonReaderException -{“解析值时遇到的意外字符:<. Path‘,第0行,位置0”} StackTrace - at Newtonsoft.Json.JsonTextReader.ParseValue() 在Newtonsoft.Json.JsonTextReader.Read() 在Newtonsoft.Json.JsonReader.ReadAndMoveToContent() 在Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType(JsonReader阅读器,JsonContract合同,布尔hasConverter) 在Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader阅读器中,输入objectType,布尔checkAdditionalContent) 在Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader阅读器上,输入objectType) 在Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader阅读器上,输入objectType) 在Newtonsoft.Json.JsonConvert.DeserializeObject(String值,类型,JsonSerializerSettings设置) 在Newtonsoft.Json.JsonConvert.DeserializeObjectT 在Google.Apis.Json.NewtonsoftJsonSerializer.DeserializeT ( C:\Apiary\v1.21\google-api-dotnet-client\Src\Support\GoogleApis.Core\Apis\Json\NewtonsoftJsonSerializer.cs:line 148 ) 在Google.Apis.Services.BaseClientService.d__34.MoveNext() ( C:\Apiary\v1.21\google-api-dotnet-client\Src\Support\GoogleApis\Apis\Services\BaseClientService.cs:line 288 )
发布于 2017-03-14 08:55:01
您的代码对我来说很好,我认为您可能存在的唯一问题是,labelsToAdd.Add("Label_1");
可能不是标签id。但是,当我尝试发送无效的标签Id时,我将得到
无效标签: adfadsfdsf 400
这是我使用的代码。
身份验证
/// <summary>
/// This method requests Authentcation from a user using Oauth2.
/// Credentials are stored in System.Environment.SpecialFolder.Personal
/// Documentation https://developers.google.com/accounts/docs/OAuth2
/// </summary>
/// <param name="clientSecretJson">Path to the client secret json file from Google Developers console.</param>
/// <param name="userName">Identifying string for the user who is being authentcated.</param>
/// <returns>DriveService used to make requests against the Drive API</returns>
public static GmailService AuthenticateOauthGmail(string clientSecretJson, string userName)
{
try
{
if (string.IsNullOrEmpty(userName))
throw new ArgumentNullException("userName");
if (string.IsNullOrEmpty(clientSecretJson))
throw new ArgumentNullException("clientSecretJson");
if (!File.Exists(clientSecretJson))
throw new Exception("clientSecretJson file does not exist.");
// These are the scopes of permissions you need. It is best to request only what you need and not all of them
string[] scopes = new string[] { GmailService.Scope.GmailSettingsSharing, //Manage your sensitive mail settings, including who can manage your mail
GmailService.Scope.GmailSettingsBasic, //Manage your basic mail settings
GmailService.Scope.GmailReadonly, //View your emails messages and settings
GmailService.Scope.GmailCompose, //Manage drafts and send emails
GmailService.Scope.GmailInsert, //Insert mail into your mailbox
GmailService.Scope.GmailLabels, //Manage mailbox labels
GmailService.Scope.GmailModify, //View and modify but not delete your email
GmailService.Scope.GmailSend, //Send email on your behalf
GmailService.Scope.MailGoogleCom}; //View and manage your mail
UserCredential credential;
using (var stream = new FileStream(clientSecretJson, FileMode.Open, FileAccess.Read))
{
string credPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials/", System.Reflection.Assembly.GetExecutingAssembly().GetName().Name);
// Requesting Authentication or loading previously stored authentication for userName
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets,
scopes,
userName,
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
}
// Create Drive API service.
return new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Gmail Oauth2 Authentication Sample"
});
}
catch (Exception ex)
{
Console.WriteLine("Create Oauth2 account GmailService failed" + ex.Message);
throw new Exception("CreateServiceAccountGmailFailed", ex);
}
}
请求。
// find the label I want to use.
var findLabel = serviceGmail.Users.Labels.List("me").Execute().Labels.Where(a => a.Name.ToLower().Equals("daimto")).FirstOrDefault();
// search for the mails that I want to add it to
var request = serviceGmail.Users.Messages.List("me");
request.Q = "from:stuff@daimto.com";
request.Execute();
var allmails = request.Execute();
ModifyMessageRequest mods = new ModifyMessageRequest();
mods.AddLabelIds = new List<string> { allLabels.Id };
// loop though each email.
foreach (var item in allmails.Messages)
{
var result = serviceGmail.Users.Messages.Modify(mods, "me", item.Id).Execute();
}
我使用的是1.19.0.694的Gmail.dll代码来自我的示例项目gmail v1示例
https://stackoverflow.com/questions/42780871
复制相似问题