C# uygulamasında, bir kullanıcı için bir Google Apps "hesabına" erişebilen bir masaüstü uygulaması yazıyorum. Takvim API'sını ve Yönetici SDK'sını "projem" e ekledim.Bir masaüstü uygulaması için birden fazla Google erişim izni var
Her iki yöntem de (kendileri) iyi çalışıyor ancak her iki API için de uygulamamı yetkilendirmek istediğimde aşağıdaki izin hatalarını alıyorum.
Yetersiz İzin [403]
invalid_grant "Açıklama:" Token iptal edildi.
Bu, "özellikler" in ayrı olarak yetkilendirilmesi yerine, uygulama başladığında tüm izinlerin sorulmasının mümkün olup olmadığına şaşırmamı sağladı mı?
static string[] CalendarScopes = {CalendarService.Scope.CalendarReadonly };
static string[] DirectoryScopes = { DirectoryService.Scope.AdminDirectoryUserReadonly };
private static void GoogleCalendar()
{
UserCredential credential;
using (var stream = new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
{
string credPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
CalendarScopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
}
// Create Google Calendar API service.
var service = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
// Define parameters of request.
EventsResource.ListRequest request = service.Events.List("primary");
request.TimeMin = DateTime.Now;
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.DateTime.ToString();
if (String.IsNullOrEmpty(when))
{
when = eventItem.Start.Date;
}
Console.WriteLine("{0} ({1})", eventItem.Summary, when);
}
}
else
{
Console.WriteLine("No upcoming events found.");
}
Console.Read();
}
private static void GoogleDirectory()
{
UserCredential credential;
using (var stream =
new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
{
string credPath = Environment.GetFolderPath(
Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
DirectoryScopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
// Create Directory API service.
var service = new DirectoryService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
// Define parameters of request.
UsersResource.ListRequest request = service.Users.List();
request.Customer = "my_customer";
request.MaxResults = 10;
request.OrderBy = UsersResource.ListRequest.OrderByEnum.Email;
// List users.
IList<User> users = null;
try
{
users = request.Execute().UsersValue;
}
catch (Exception ex)
{
throw;
}
Console.WriteLine("Users:");
if (users != null && users.Count > 0)
{
foreach (var userItem in users)
{
Console.WriteLine("{0} ({1})", userItem.PrimaryEmail,
userItem.Name.FullName);
}
}
else
{
Console.WriteLine("No users found.");
}
Console.Read();
}
Aldığınız hatalar nelerdir? Evet, uygulamanız için gerekecek tüm kapsamlara sahip bir erişim belirteci isteyebilirsiniz. Bu tavsiye edilmemesine rağmen. – Gerardo
Sorgumu istisnalarla güncelledim. Neden birden fazla kapsam için bir erişim belirteci kullanması önerilmez? –