GoogleWebAuthorizationBroker.AuthorizeAsync抛出错误

本文关键字:错误 出错 AuthorizeAsync GoogleWebAuthorizationBroker | 更新日期: 2023-09-27 18:11:00

我试图创建和运行这个链接中给出的示例项目。下面是代码:

namespace GmailQuickstart
{
    class Program
    {
        static string[] Scopes = { GmailService.Scope.GmailReadonly };
        static string ApplicationName = "Gmail API Quickstart";
        static void Main(string[] args)
        {
            UserCredential credential;
            using (var stream =
                new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
            {
                string credPath = System.Environment.GetFolderPath(
                    System.Environment.SpecialFolder.Personal);
                credPath = Path.Combine(credPath, ".credentials");
                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    Scopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(credPath, true)).Result;
                Console.WriteLine("Credential file saved to: " + credPath);
            }
            // Create Gmail API service.
            var service = new GmailService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName = ApplicationName,
                });
            // Define parameters of request.
            UsersResource.LabelsResource.ListRequest request = service.Users.Labels.List("me");
            // List labels.
            IList<Label> labels= request.Execute().Labels;
            Console.WriteLine("Labels:");
            if (labels != null && labels.Count > 0)
            {
                foreach (var labelItem in labels)
                {
                    Console.WriteLine("{0}", labelItem.Name);
                }
            }
            else
            {
                Console.WriteLine("No labels found.");
            }
            Console.Read();
        }
    }
}

在我完成了google文档中所述的一切之后,包括发布一个秘密。我运行了应用程序/产品的Json文件。
它失败并产生一个异常,包含一个"提供了无效参数"的内部异常。
我已经用新的api凭据等新帐户重新尝试了所有内容,并且仍然发生相同的错误。我做错了什么?

GoogleWebAuthorizationBroker.AuthorizeAsync抛出错误

试试这个

ClientSecrets secrets = new ClientSecrets()
{
    ClientId = CLIENT_ID,
    ClientSecret = CLIENT_SECRET
};
var token = new TokenResponse { RefreshToken = REFRESH_TOKEN }; 
var credentials = new UserCredential(new GoogleAuthorizationCodeFlow(
    new GoogleAuthorizationCodeFlow.Initializer 
    {
        ClientSecrets = secrets
    }), 
    "user", 
    token);
var service = new GmailService(new BaseClientService.Initializer()
{
    HttpClientInitializer = credentials,
    ApplicationName = "ApplicationName"
});
相关文章: