Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Gmail API服务帐户_C#_Oauth 2.0_Gmail Api_Service Accounts - Fatal编程技术网

C# Gmail API服务帐户

C# Gmail API服务帐户,c#,oauth-2.0,gmail-api,service-accounts,C#,Oauth 2.0,Gmail Api,Service Accounts,我想用gmail API阅读我的gmail收件箱。我需要使用服务帐户,因为我的应用程序没有用户交互。 我在请求时收到以下错误: “InnerException={”错误:\“unauthorized\u client\”,描述:\“请求中的未授权客户端或作用域。\”,Uri:\“\”} 这是我的代码: string applicationName = "Gmail API .NET"; string[] scopes = { GmailService.Scope.

我想用gmail API阅读我的gmail收件箱。我需要使用服务帐户,因为我的应用程序没有用户交互。 我在请求时收到以下错误:

“InnerException={”错误:\“unauthorized\u client\”,描述:\“请求中的未授权客户端或作用域。\”,Uri:\“\”}

这是我的代码:

        string applicationName = "Gmail API .NET";
        string[] scopes = { GmailService.Scope.GmailReadonly };

        string certPath = "./XXXXXXXXXX.p12";
        string userEmail = "MYEMAIL@gmail.com";
        string serviceAccountEmail = "MYSERVICEACCOUNT...am.gserviceaccount.com";

        //Carga el certificado obtenido de 
        var certificate = new X509Certificate2(certPath, "notasecret", X509KeyStorageFlags.Exportable);

        ServiceAccountCredential credential = new ServiceAccountCredential(
            new ServiceAccountCredential.Initializer(serviceAccountEmail)
            {
                User = userEmail,
                Scopes = scopes
            }.FromCertificate(certificate)
        );

        if (credential.RequestAccessTokenAsync(CancellationToken.None).Result) <--- Here I get the error
        {
            GmailService gs = new GmailService(
                new BaseClientService.Initializer()
                {
                    ApplicationName = applicationName,
                    HttpClientInitializer = credential
                }
            );
        }
string applicationName=“Gmail API.NET”;
字符串[]范围={GmailService.Scope.GmailReadonly};
字符串certPath=“./xxxxxxxxx.p12”;
字符串userEmail=”MYEMAIL@gmail.com";
字符串serviceAccountEmail=“MYSERVICEACCOUNT…am.gserviceaccount.com”;
//认证卡
var证书=新的X509Certificate2(certPath,“notasecret”,X509KeyStrageFlags.Exportable);
ServiceAccountCredential credential=新ServiceAccountCredential(
新ServiceAccountCredential.初始值设定项(serviceAccountEmail)
{
User=userEmail,
范围=范围
}.FromCertificate(证书)
);

if(credential.RequestAccessTokenAsync(CancellationToken.None.Result)您是否尝试过Google提供的用于此函数的示例代码

  using Google.Apis.Gmail.v1;
 using Google.Apis.Gmail.v1.Data;

 // ...

 public class MyClass {

   // ...

 /// <summary>
 /// Retrieve a Message by ID.
 /// </summary>
 /// <param name="service">Gmail API service instance.</param>
 /// <param name="userId">User's email address. The special value "me"
 /// can be used to indicate the authenticated user.</param>
 /// <param name="messageId">ID of Message to retrieve.</param>
 public static Message GetMessage(GmailService service, String userId, String messageId)
 {
     try
     {
         return service.Users.Messages.Get(userId, messageId).Execute();
     }
     catch (Exception e)
     {
         Console.WriteLine("An error occurred: " + e.Message);
     }

     return null;
 }

 // ...

}
使用Google.api.Gmail.v1;
使用Google.api.Gmail.v1.Data;
// ...
公共类MyClass{
// ...
/// 
///按ID检索消息。
/// 
///Gmail API服务实例。
///用户的电子邮件地址。特殊值“我”
///可用于指示经过身份验证的用户。
///要检索的消息的ID。
公共静态消息GetMessage(Gmail服务,字符串用户ID,字符串消息ID)
{
尝试
{
return service.Users.Messages.Get(userId,messageId.Execute();
}
捕获(例外e)
{
Console.WriteLine(“发生错误:+e.Message”);
}
返回null;
}
// ...
}
您是否在此处尝试过API资源管理器:
并输入您的请求信息?它在API页面上工作了吗?

您是否尝试过Google提供的用于此功能的示例代码

  using Google.Apis.Gmail.v1;
 using Google.Apis.Gmail.v1.Data;

 // ...

 public class MyClass {

   // ...

 /// <summary>
 /// Retrieve a Message by ID.
 /// </summary>
 /// <param name="service">Gmail API service instance.</param>
 /// <param name="userId">User's email address. The special value "me"
 /// can be used to indicate the authenticated user.</param>
 /// <param name="messageId">ID of Message to retrieve.</param>
 public static Message GetMessage(GmailService service, String userId, String messageId)
 {
     try
     {
         return service.Users.Messages.Get(userId, messageId).Execute();
     }
     catch (Exception e)
     {
         Console.WriteLine("An error occurred: " + e.Message);
     }

     return null;
 }

 // ...

}
使用Google.api.Gmail.v1;
使用Google.api.Gmail.v1.Data;
// ...
公共类MyClass{
// ...
/// 
///按ID检索消息。
/// 
///Gmail API服务实例。
///用户的电子邮件地址。特殊值“我”
///可用于指示经过身份验证的用户。
///要检索的消息的ID。
公共静态消息GetMessage(Gmail服务,字符串用户ID,字符串消息ID)
{
尝试
{
return service.Users.Messages.Get(userId,messageId.Execute();
}
捕获(例外e)
{
Console.WriteLine(“发生错误:+e.Message”);
}
返回null;
}
// ...
}
您是否在此处尝试过API资源管理器: 并输入您的请求信息?它在API页面上起作用了吗?

试着检查一下.NET库中的服务帐户。本文档还为您提供了一个示例代码,您可以根据该代码了解如何设置服务帐户。这还可以让您了解如何使用服务帐户访问GMAIL API

现在,对于您收到的错误,请检查此链接是否可以帮助您

尝试检查有关.NET库中服务帐户的信息。本文档还为您提供了一个示例代码,您可以根据该代码了解如何设置服务帐户。这还可以让您了解如何使用服务帐户访问GMAIL API

现在,对于您收到的错误,请检查此链接是否可以帮助您


服务帐户无法访问@gmail.com邮箱。您必须使用中描述的其他受支持的OAuth 2.0授权方案之一


有关详细信息。

服务帐户无法访问@gmail.com邮箱。您必须使用中描述的其他受支持的OAuth 2.0授权方案之一


有关更多详细信息。

您只能使用服务帐户为GSuite帐户而不是gmail帐户发送电子邮件

如果你有一个gmail帐户,你可以使用 或者打开2FA,生成一个并使用它,如图所示


如果您使用的是GSuite帐户,您可以使用ServiceAccount,但您必须确保它具有所述的G套件域范围的授权,然后您需要按照所述授予对GSuite域的访问权

您只能使用服务帐户为GSuite帐户而不是gmail帐户发送电子邮件

如果你有一个gmail帐户,你可以使用 或者打开2FA,生成一个并使用它,如图所示


如果您使用的是GSuite帐户,您可以使用ServiceAccount,但您必须确保它具有所述的G套件域范围的授权,然后您需要按照所述授予对GSuite域的访问权

您需要配置您的Gmail帐户以允许POP3/SMTP访问。你可以在线在你的Gmail设置中这样做。谢谢。我这样做了,但我得到了相同的结果:(试试这个。你还需要G套件来使用服务帐户。你需要配置你的Gmail帐户以允许POP3/SMTP访问。你可以在线在Gmail设置中执行此操作。谢谢。我这样做了,但我得到了相同的结果:(试试这个。你还需要G套件来使用服务帐户。从Google测试可以很好地工作。我认为问题在于身份验证。当我调用此方法credential.RequestAccessTokenAsync时,我得到这个错误{“error:\“unauthorized\u client\”,Description:\“unauthorized client or scope in request.\”,Uri:\“\”)(CancellationToken.None)。Google测试的结果工作正常。我认为问题在于身份验证。当我调用此方法credential.RequestAccessTokenAsync(CancellationToken.None)时,我得到了以下错误{“错误:\'unauthorized\'u client\”,描述:\'unauthorized client or scope in request.\”,Uri:\“\”}.ResultI设法使用一个旧的Gmail帐户。测试是用一个新帐户完成的。如果旧帐户