运行代码以使用c#访问gmail API时获取filenotfound异常 [英] Getting filenotfound exception while running code to access gmail API using c#
本文介绍了运行代码以使用c#访问gmail API时获取filenotfound异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
运行代码以使用c#访问gmail API时出现filenotfound异常。我刚刚使用了google开发人员指南中提供的代码。请参阅下面的代码。
使用Google.Apis.Auth.OAuth2;
使用Google.Apis.Gmail.v1;
使用Google.Apis.Gmail.v1.Data;
使用Google.Apis.Services;
使用Google.Apis.Util.Store;
使用系统;
使用System.Collections.Generic;
$
使用System.IO;
使用System.Linq;
使用System.Text;
使用System.Threading;
使用System.Threading.Tasks;
命名空间GmailQuickstart
{
 课程计划
  {
      //如果要修改这些范围,请删除以前保存的凭据。
      // at~ / .credentials / gmail-dotnet-quickstart.json
      static string [] Scopes = {GmailService.Scope.GmailReadonly};
      static string ApplicationName =" Gmail API .NET Quickstart"; $
      static void Main(string [] args)
      {
          UserCredential凭证;
         使用(VAR流=
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;新的FileStream(QUOT; client_secret.json" ;, FileMode.Open,FileAccess.Read))<无线电通信/>
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;串credPath = System.Environment.GetFolderPath(
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; System.Environment.SpecialFolder.Personal);
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; credPath = Path.Combine(credPath," .credentials / gmail-dotnet-quickstart.json");
$
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;作用域,
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;"使用者" ;,
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; CancellationToken.None,
&NBSP;&NBSP; new FileDataStore(credPath,true))。结果;
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Console.WriteLine("凭据文件保存到:" + credPath";)
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //创建Gmail API服务。
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; var service = new GmailService(new BaseClientService.Initializer()
{
;&NBSP;&NBSP;&NBSP; HttpClientInitializer =凭证,
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;应用程序名=应用程序名,
&NBSP;&NBSP ; });
$
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //定义请求参数。
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; UsersResource.LabelsResource.ListRequest request = service.Users.Labels.List(" me");
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //列出标签。
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP;的IList<标签> labels = request.Execute()。标签;
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Console.WriteLine(" Labels:");
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; if(labels!= null&& labels.Count> 0)
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; foreach(标签中的var labelItem)
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Console.WriteLine(" {0}",labelItem.Name);
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;否则
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Console.WriteLine("未找到标签。");
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Console.Read();
$
&NBSP; &NBSP; &NBSP; }¥b $ b &NBSP; }
}
尝试访问文件时发现文件未找到异常"client_secret.json" .Error在读取文件时执行并在行下面执行
var stream = &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; new FileStream(" client_secret.json",FileMode.Open,FileAccess.Read)
如何解决问题
解决方案
尝试提供所述路径例如
<预类= "prettyprint" 风格= "">变种文件名= Path.Combine(AppDomain.CurrentDomain.BaseDirectory," client_secret.json");
if(File.Exists(fileName))
{
var stream = new FileStream(fileName,FileMode.Open,FileAccess.Read)
}
调整路径以满足您的需求。
Getting filenotfound exception while running code to access gmail API using c#.I have just used the code available in google developers guide.Please see below is the code.
using Google.Apis.Auth.OAuth2;
using Google.Apis.Gmail.v1;
using Google.Apis.Gmail.v1.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace GmailQuickstart
{
class Program
{
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/gmail-dotnet-quickstart.json
static string[] Scopes = { GmailService.Scope.GmailReadonly };
static string ApplicationName = "Gmail API .NET 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/gmail-dotnet-quickstart.json");
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();
}
}
}
File not found exception throwing when trying to access the file "client_secret.json" .Error throws while reading the file and performing below line
var stream = new FileStream("client_secret.json", FileMode.Open, FileAccess.Read)
How to resolve the issue
解决方案
Try providing the path e.g.
var fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "client_secret.json"); if (File.Exists(fileName)) { var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read) }Adjust the path to fit your needs.
这篇关于运行代码以使用c#访问gmail API时获取filenotfound异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文