2017-04-20 91 views
0

我開始研究Gmail API。我按照教程顯示標籤列表(https://developers.google.com/gmail/api/quickstart/dotnet),並且工作正常。Gmail API - 值爲'=獲取標籤google-api-dotnet-client/1.25.0.0(gzip)'的格式無效

HELP is highly appreciated

當我修改程序流程時,在這裏它給了我錯誤。我無法追查錯誤。它給我錯誤Execute()方法。

Error: The format of value '= Get Labels google-api-dotnet-client/1.25.0.0 (gzip)' is invalid

這是我的代碼。

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

這裏的主要功能是

class GmailMailBox 
{ 
    static void Main (string [ ] args) 
    { 
     Labels.ListLabels(); 

     Console .WriteLine ("Press key to exit ..."); 
     Console .Read (); 
    } 
} 

給了我這個錯誤。 error picture

回答

3

這幾乎肯定是由於"AppName = Get Labels"應用程序名稱。 更改此刪除空格和'=',我懷疑這個錯誤將消失。