使用Google日历api

时间:2017-02-01 10:03:31

标签: c# asp.net-mvc-4 google-api google-calendar-api google-api-dotnet-client

我正在使用谷歌日历API服务来访问用户日历,它在我的本地工作正常,但它在下面的服务器中工作是在我当地工作正常的代码。

public ActionResult AddGoogleEvent()
{          
    UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
        new ClientSecrets
        {
            ClientId = clientid,
           ClientSecret = clientsecret,
        },
        new[] { CalendarService.Scope.Calendar },
        "user",
        CancellationToken.None).Result;

    var service = new CalendarService(new BaseClientService.Initializer(){
        HttpClientInitializer = credential,
        ApplicationName = "sampleappilication"
        });

    Google.Apis.Calendar.v3.Data.Event event1 = new Google.Apis.Calendar.v3.Data.Event()
    {
        Summary = "Appointment",
        Location = location,
        Start = new Google.Apis.Calendar.v3.Data.EventDateTime()
        {
            DateTime = new DateTime(Convert.ToInt32(yy), Convert.ToInt32(mn), Convert.ToInt32(dy), Convert.ToInt32(sthour), Convert.ToInt32(stminute), 0),
            TimeZone = location
        },
        End = new Google.Apis.Calendar.v3.Data.EventDateTime()
        {
            DateTime = new DateTime(Convert.ToInt32(yy), Convert.ToInt32(mn), Convert.ToInt32(dy), Convert.ToInt32(ethour), Convert.ToInt32(etminute), 0),
            TimeZone = "America/Los_Angeles"
        },
        Recurrence = new String[] {"RRULE:FREQ=WEEKLY;BYDAY=MO"},
        Attendees = new List<Google.Apis.Calendar.v3.Data.EventAttendee>()
        {
            new Google.Apis.Calendar.v3.Data.EventAttendee() { Email = attendencess }
        }
    };

    Google.Apis.Calendar.v3.Data.Event thisevent = service.Events.Insert(event1, "primary").Execute(); // Another error. "Does not contain a definition for Fetch"

    string newEventID = thisevent.Id;

    Session["Accepted"] = "Accepted";
    return RedirectToAction("Eventconfirm");
}

异常

  

拒绝访问“Google.Apis.Auth”路径。堆栈跟踪:在   Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSu CCESS(Tasktask)自性rosoft.Runtime.Compi lerServices.TaskAwai ter.HandleNonSuccess(任务   任务)在   Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker.d__1.MoveNe XT()   在   C:\ Users \用户mdril \文件\ GitHub的\ Google处理API-DOTNET客户端\ SRC \ GoogleApis.Auth.Dot NET4 \的OAuth2 \ GoogleWe bAuthorizationBroker的.cs:线   59来源:Microsoft.Threading.Tasks TargetSite:Void   ThrowForNonSuccess(System.Threading.Tasks.Task)

任何人都可以建议如何解决此问题

2 个答案:

答案 0 :(得分:1)

UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
        new ClientSecrets
        {
            ClientId = clientid,
            ClientSecret = clientsecret,
        },
        new[] { CalendarService.Scope.Calendar },
        "user",
        CancellationToken.None).Result;

默认情况下,代码使用FileDataStore来存储您的凭据。 FileDataStore默认将凭据存储在%appData%中。我可以找到关于filedatastore的教程here

  

拒绝访问“Google.Apis.Auth”路径

可能意味着您的服务器无权写入该路径。您可以执行以下操作来更改凭据的存储路径。

UserCredential credential;
using (var stream = new FileStream(clientSecretsJsonFilePath
                               ,FileMode.Open
                               ,FileAccess.Read))
  {   
  credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
  GoogleClientSecrets.Load(stream).Secrets,
  new[] { CalendarService.Scope.Calendar },
  "LookIAmAUniqueUser",
   CancellationToken.None,
  new FileDataStore(@"c:\datastore",true)                              
  ).Result;
  }

或者,您也可以创建自己的idatastore实现并按照自己喜欢的方式存储凭据

答案 1 :(得分:1)

GoogleWebAuthorizationBroker.AuthorizeAsync可在控制台应用中运行(或在开发计算机中本地使用),但对于Web应用,您需要网络流。

请检查此链接:

http://www.c-sharpcorner.com/article/implementing-oauth2-0-authorization-for-google-in-asp-net/

https://stackoverflow.com/a/46604699/6080079

第一个是获取accessToken,第二个是从该令牌获取服务。

希望这有帮助!