如何访问 google 日历的客户端机密文件
How to access client secret file of google calendar
我想访问 google 日历的 client_secret 文件并使用它在我的日历中添加一个事件。
我正在用 c# 创建一个聊天机器人,在我使用我的计算机路径的那一刻(当我尝试访问我的 pc 中项目的 client_secret 文件时)工作正常。
当我在网络上发布项目并尝试访问我无法访问的文件时。
我的代码是:
string path = @"C:\Project\Test\Bot\V3\client_secret.json";
string pathi = @"{
'installed': {
'client_id': '1111111-
21432423.apps.googleusercontent.com',
'project_id': '111',
'auth_uri': 'https://accounts.google.com/o/oauth2/auth',
'token_uri': 'https://www.googleapis.com/oauth2/v3/token',
'auth_provider_x509_cert_url': 'https://www.googleapis.com/oauth2/v1/certs',
'client_secret': '22222222222222',
'redirect_uris': [ 'urn:ietf:wg:oauth:2.0:oob', 'http:dev.ai.com/BotTest/' ]
}
}";
UserCredential credential;
string[] Scopes = { CalendarService.Scope.Calendar };
string credPath = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal);
using (var stream =
new FileStream(pathi, FileMode.Open, FileAccess.Read))
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
string ApplicationName = "Google Calendar API .NET Quickstart";
var service = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
Event newEvent = new Event()
{
Summary = "Event",
Location = "",
Description = "Event is scheduled for: " +
eventi.Date.ToString("dd-MM-yyyy") + " " +
eventi.Time.ToShortTimeString(),
Start = new EventDateTime()
{
// DateTime = eventi.Date,
DateTime = new DateTime(eventi.Date.Year,
eventi.Date.Month, eventi.Date.Day, eventi.Time.Hour,
eventi.Time.Minute, 0),
TimeZone = "Europe/Brussels",
},
End = new EventDateTime()
{
DateTime = eventi.Date.AddMinutes(60),
// DateTime = new DateTime(eventi.Date.Year,
eventi.Date.Month, eventi.Date.Day, eventi.Time.Hour,
eventi.Time.Hour +1 ,0),
TimeZone = "Europe/Brussels",
},
Recurrence = new String[] { "RRULE:FREQ=DAILY;COUNT=1" },
Attendees = new EventAttendee[] {
new EventAttendee() { Email = "eeee@gmail.com" },
},
///////////////////////////////////////////
///////////////////////////////////////////
Reminders = new Event.RemindersData()
{
UseDefault = false,
Overrides = new EventReminder[] {
new EventReminder() { Method = "email", Minutes = 1 * 60 },
new EventReminder() { Method = "sms", Minutes = 10 },
}
}
};
String calendarId = "primary";
EventsResource.InsertRequest request1 =
service.Events.Insert(newEvent, calendarId);
Event createdEvent = request1.Execute();
Console.WriteLine("Event created: {0}", createdEvent.HtmlLink);
// Define parameters of request.
EventsResource.ListRequest request = service.Events.List("primary");
request.TimeMin = eventi.Time;
request.ShowDeleted = false;
request.SingleEvents = true;
request.MaxResults = 10;
request.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;
// List events.
Events events = request.Execute();
Console.WriteLine("Upcoming events:");
if (events.Items != null && events.Items.Count > 0)
{
foreach (var eventItem in events.Items)
{
string when = eventItem.Start.Date;
if (String.IsNullOrEmpty(when))
{
when = eventItem.Start.Date;
}
Console.WriteLine("{0} ({1})", eventItem.Summary, when);
}
}
else
{
Console.WriteLine("No upcoming events found.");
}
//Console.Read();
// context.Wait(this.MessageReceived);
}
context.Wait(this.MessageReceived);
}
当我使用路径时,在这部分代码中我将路径工作:
using (var stream =
new FileStream(path, FileMode.Open, FileAccess.Read))
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
当我将文件的根目录放在网络中时不起作用,当我将文件的所有脚本放在字符串路径中时也是如此:
string path = @"http://dev.aaaa.com/bot/client_secret.json"
我尝试只放入名称文件,但仍然不起作用:
using (var stream =
new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
你能帮我看看如何访问这个文件吗?
将 credentials.json 复制到 bin\debug 和 bin\release 文件夹,如果您的客户端是控制台应用程序,请按照此方式操作,
UserCredential credential;
string credPath = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal);
using (var stream = new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
{
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
new[] { CalendarService.Scope.CalendarReadonly },
"user", CancellationToken.None, new FileDataStore(credPath));
}
对于网络应用程序,client_secret.json 看起来像这样并包含重定向 uri 等。
{"web":{"client_id":" ","project_id":" ","auth_uri":" ","token_uri":" ","auth_provider_x509_cert_url":" ","client_secret":" ","redirect_uris":[]}}
也不要泄露您的任何身份和秘密。
我想访问 google 日历的 client_secret 文件并使用它在我的日历中添加一个事件。
我正在用 c# 创建一个聊天机器人,在我使用我的计算机路径的那一刻(当我尝试访问我的 pc 中项目的 client_secret 文件时)工作正常。
当我在网络上发布项目并尝试访问我无法访问的文件时。
我的代码是:
string path = @"C:\Project\Test\Bot\V3\client_secret.json";
string pathi = @"{
'installed': {
'client_id': '1111111-
21432423.apps.googleusercontent.com',
'project_id': '111',
'auth_uri': 'https://accounts.google.com/o/oauth2/auth',
'token_uri': 'https://www.googleapis.com/oauth2/v3/token',
'auth_provider_x509_cert_url': 'https://www.googleapis.com/oauth2/v1/certs',
'client_secret': '22222222222222',
'redirect_uris': [ 'urn:ietf:wg:oauth:2.0:oob', 'http:dev.ai.com/BotTest/' ]
}
}";
UserCredential credential;
string[] Scopes = { CalendarService.Scope.Calendar };
string credPath = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal);
using (var stream =
new FileStream(pathi, FileMode.Open, FileAccess.Read))
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
string ApplicationName = "Google Calendar API .NET Quickstart";
var service = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
Event newEvent = new Event()
{
Summary = "Event",
Location = "",
Description = "Event is scheduled for: " +
eventi.Date.ToString("dd-MM-yyyy") + " " +
eventi.Time.ToShortTimeString(),
Start = new EventDateTime()
{
// DateTime = eventi.Date,
DateTime = new DateTime(eventi.Date.Year,
eventi.Date.Month, eventi.Date.Day, eventi.Time.Hour,
eventi.Time.Minute, 0),
TimeZone = "Europe/Brussels",
},
End = new EventDateTime()
{
DateTime = eventi.Date.AddMinutes(60),
// DateTime = new DateTime(eventi.Date.Year,
eventi.Date.Month, eventi.Date.Day, eventi.Time.Hour,
eventi.Time.Hour +1 ,0),
TimeZone = "Europe/Brussels",
},
Recurrence = new String[] { "RRULE:FREQ=DAILY;COUNT=1" },
Attendees = new EventAttendee[] {
new EventAttendee() { Email = "eeee@gmail.com" },
},
///////////////////////////////////////////
///////////////////////////////////////////
Reminders = new Event.RemindersData()
{
UseDefault = false,
Overrides = new EventReminder[] {
new EventReminder() { Method = "email", Minutes = 1 * 60 },
new EventReminder() { Method = "sms", Minutes = 10 },
}
}
};
String calendarId = "primary";
EventsResource.InsertRequest request1 =
service.Events.Insert(newEvent, calendarId);
Event createdEvent = request1.Execute();
Console.WriteLine("Event created: {0}", createdEvent.HtmlLink);
// Define parameters of request.
EventsResource.ListRequest request = service.Events.List("primary");
request.TimeMin = eventi.Time;
request.ShowDeleted = false;
request.SingleEvents = true;
request.MaxResults = 10;
request.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;
// List events.
Events events = request.Execute();
Console.WriteLine("Upcoming events:");
if (events.Items != null && events.Items.Count > 0)
{
foreach (var eventItem in events.Items)
{
string when = eventItem.Start.Date;
if (String.IsNullOrEmpty(when))
{
when = eventItem.Start.Date;
}
Console.WriteLine("{0} ({1})", eventItem.Summary, when);
}
}
else
{
Console.WriteLine("No upcoming events found.");
}
//Console.Read();
// context.Wait(this.MessageReceived);
}
context.Wait(this.MessageReceived);
}
当我使用路径时,在这部分代码中我将路径工作:
using (var stream =
new FileStream(path, FileMode.Open, FileAccess.Read))
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
当我将文件的根目录放在网络中时不起作用,当我将文件的所有脚本放在字符串路径中时也是如此:
string path = @"http://dev.aaaa.com/bot/client_secret.json"
我尝试只放入名称文件,但仍然不起作用:
using (var stream =
new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
你能帮我看看如何访问这个文件吗?
将 credentials.json 复制到 bin\debug 和 bin\release 文件夹,如果您的客户端是控制台应用程序,请按照此方式操作,
UserCredential credential;
string credPath = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal);
using (var stream = new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
{
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
new[] { CalendarService.Scope.CalendarReadonly },
"user", CancellationToken.None, new FileDataStore(credPath));
}
对于网络应用程序,client_secret.json 看起来像这样并包含重定向 uri 等。
{"web":{"client_id":" ","project_id":" ","auth_uri":" ","token_uri":" ","auth_provider_x509_cert_url":" ","client_secret":" ","redirect_uris":[]}}
也不要泄露您的任何身份和秘密。