简体   繁体   中英

c# Google Calendar API V3 is ignoring my other account (multiple account authorization)

I have been struggling with Google Calendar Api (V3) for quite a while, but I can now create events and list events for a given timespan. I am doing this in C# to create a desktop app.

I have two different google accounts, and I want to be able to post to each of those calendars separately. Using the code below (for the first google account) I am able to connect and post to the one calendar, but when I use the same code on a different form with the .json file from the 2nd google account, it comes back with error [404] not found when I attempt to list the events in that particular calendar. It is able to show a list of calendars BUT the ones listed are from the 1st google account. I have logged out of my google accounts in my web-browser, but my program consistently things I am working with the 1st google account.

PS: for the 2nd google account, I have a differntly named .json file, downloaded from the developer site while logged in with the 2nd google account. The app name for the 2nd google account is also named differently.

    public async Task<int> testZZquery()   
    {
        UserCredential credential;
        using (var stream = new FileStream("CMgcal_client_secrets.json", FileMode.Open, FileAccess.Read))
        {
            credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                new[] { CalendarService.Scope.Calendar },
                "user", CancellationToken.None);
        }

        // Create the service.
        var service = new CalendarService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "mygcal",
        });

It's the Oauth tokens that decide which account will be accessed, it's not the client secret or the project you are using. You can happily use the same project in the console for retrieving data from both the accounts. However, you will need to request a separate OAuth token for each of them and then use it for accessing the data.

The Oauth2 documentation is here: https://developers.google.com/accounts/docs/OAuth2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM