C# 你向下滚动到底部,有一个webapp链接,也许这就是你需要的?这个示例非常愚蠢,因为它在文件中存储内容,并且对我隐藏内容。我宁愿使用这个图书馆也不愿休息。他们把这个图书馆设计得如此糟糕,真令人讨厌。 using Google.Apis.Auth.OAuth

C# 你向下滚动到底部,有一个webapp链接,也许这就是你需要的?这个示例非常愚蠢,因为它在文件中存储内容,并且对我隐藏内容。我宁愿使用这个图书馆也不愿休息。他们把这个图书馆设计得如此糟糕,真令人讨厌。 using Google.Apis.Auth.OAuth,c#,xamarin,uwp,google-calendar-api,C#,Xamarin,Uwp,Google Calendar Api,你向下滚动到底部,有一个webapp链接,也许这就是你需要的?这个示例非常愚蠢,因为它在文件中存储内容,并且对我隐藏内容。我宁愿使用这个图书馆也不愿休息。他们把这个图书馆设计得如此糟糕,真令人讨厌。 using Google.Apis.Auth.OAuth2; using Google.Apis.Calendar.v3; using Google.Apis.Calendar.v3.Data; using Google.Apis.Services; using Google.Apis.Util.S


你向下滚动到底部,有一个webapp链接,也许这就是你需要的?这个示例非常愚蠢,因为它在文件中存储内容,并且对我隐藏内容。我宁愿使用这个图书馆也不愿休息。他们把这个图书馆设计得如此糟糕,真令人讨厌。
using Google.Apis.Auth.OAuth2;
using Google.Apis.Calendar.v3;
using Google.Apis.Calendar.v3.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace TestXamarinFormsLibrary.Pages
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class CalendarPage : ContentPage
    {
        public CalendarPage()
        {
            InitializeComponent();
            Go();
        }

        static string[] Scopes = { CalendarService.Scope.CalendarReadonly };
        static string ApplicationName = "Google Calendar API .NET Quickstart";

        private async void Go()
        {
            UserCredential credential;

            var assembly = GetType().Assembly;
            using (var stream = assembly.GetManifestResourceStream("TestXamarinFormsLibrary.client_secret.json"))
            {
                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                        GoogleClientSecrets.Load(stream).Secrets,
                        Scopes,
                        "user",
                        CancellationToken.None,
                        new DataStore()).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();
            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;
                    }
                    await DisplayAlert("ok", $"{eventItem.Summary} ({when})", "ok");
                }
            }
        }
    }

    public class DataStore : IDataStore
    {
        private Dictionary<string, object> Data = new Dictionary<string, object>();

        public Task ClearAsync()
        {
            throw new NotImplementedException();
        }

        public Task DeleteAsync<T>(string key)
        {
            throw new NotImplementedException();
        }

        public async Task<T> GetAsync<T>(string key)
        {
            if (Data.ContainsKey(key))
            {
                return (T)Data[key];
            }

            return default(T);
        }

        public async Task StoreAsync<T>(string key, T value)
        {
            Data.Add(key, value);
        }
    }
}
namespace GoogleProxy
{
public class GoogleService
{
    public CalendarService calendarService { get; private set; }

    public DriveService driveService { get; private set; }


    public GoogleService()
    {

    }

    public void Initialize(AuthResult authResult)
    {
        var credential = GetCredentialForApi(authResult);
        var baseInitializer = new BaseClientService.Initializer { HttpClientInitializer = credential, ApplicationName = "{your app name here}" };

        calendarService = new Google.Apis.Calendar.v3.CalendarService(baseInitializer);
        driveService = new Google.Apis.Drive.v3.DriveService(baseInitializer);
    }

    private UserCredential GetCredentialForApi(AuthResult authResult)
    {
        var initializer = new GoogleAuthorizationCodeFlow.Initializer
        {
            ClientSecrets = new ClientSecrets
            {
                ClientId = "{your app client id here}",
                ClientSecret = "",
            },
            Scopes = new string[] { "openid", "email", "profile",  "https://www.googleapis.com/auth/calendar.readonly", "https://www.googleapis.com/auth/calendar.events.readonly", "https://www.googleapis.com/auth/drive.readonly" },
        };

        var flow = new GoogleAuthorizationCodeFlow(initializer);

        var token = new TokenResponse()
        {
            AccessToken = authResult.AccessToken,
            RefreshToken = authResult.RefreshToken,
            ExpiresInSeconds = authResult.ExpirationInSeconds,
            IdToken = authResult.IdToken,
            IssuedUtc = authResult.IssueDateTime,
            Scope = "openid email profile https://www.googleapis.com/auth/calendar.readonly https://www.googleapis.com/auth/calendar.events.readonly https://www.googleapis.com/auth/drive.readonly",
            TokenType = "bearer" };

        return new UserCredential(flow, authResult.Id, token);
    }

}
}