问题
I started exploring Gmail API. I followed the tutorial to show labels list (https://developers.google.com/gmail/api/quickstart/dotnet), and worked fine.
HELP is highly appreciated
When I modified the flow of program, here it gives me the error. I cannot trace the error. It gives me error on Execute()
method.
Error: The format of value '= Get Labels google-api-dotnet-client/1.25.0.0 (gzip)' is invalid
here is my code.
public static class Labels
{
public static void ListLabels ( )
{
try
{
var scope = new [] { GmailService.Scope.GmailReadonly };
var service = Authorization.GetGmailService(scope, "AppName = Get Labels");
if (service != null)
{
var requestListLabels = service.Users.Labels.List("me");
var labelsList = requestListLabels.Execute().Labels;
Console .WriteLine ( "\n\n---- Labels List ----" );
if ( labelsList != null && labelsList .Count > 0 )
{
foreach ( var label in labelsList )
{
Console .WriteLine ( "{0}", label .Name );
}
}
else
{
Console .WriteLine ( "No labels available." );
}
}
else
{
Console.WriteLine("Gmail service not available.");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
throw;
}
}
}
public class Authorization
{
public object GmailAuth2 ( string[] scopes )
{
try
{
using ( var stream = new FileStream ( "Secrets/client_secret.json", FileMode .Open, FileAccess .Read ) )
{
var clientsecrets = GoogleClientSecrets .Load ( stream ) .Secrets;
var creds = GoogleWebAuthorizationBroker .AuthorizeAsync (
clientsecrets,
scopes,
"user",
CancellationToken .None,
new FileDataStore(this.GetType().ToString())
) .Result;
return creds;
}
}
catch ( Exception ex )
{
return ex .Message;
}
}
public static GmailService GetGmailService(string[] scopes, string appname)
{
try
{
var authproblem = new Authorization().GmailAuth2(scopes);
if (authproblem is string)
{
Console.WriteLine(authproblem);
return null;
}
var srvc = new GmailService(new BaseClientService.Initializer
{
HttpClientInitializer = (UserCredential)authproblem,
ApplicationName = appname
});
return srvc;
}
catch (Exception e)
{
Console.WriteLine(e);
return null;
}
}
}
here is the MAIN function
class GmailMailBox
{
static void Main ( string [ ] args )
{
Labels.ListLabels();
Console .WriteLine ( "Press key to exit ..." );
Console .Read ( );
}
}
gives me error of this. error picture
回答1:
This is almost certainly due to the "AppName = Get Labels"
application name.
Change this to remove the spaces and '=', and I suspect this error will go away.
来源:https://stackoverflow.com/questions/43518129/gmail-api-the-format-of-value-get-labels-google-api-dotnet-client-1-25-0-0