Google Calendar API和C# - 403禁止使用

问题描述 投票:3回答:1

寻求一些帮助。我已经阅读了所有文档,SO和Google,但仍然没有运气。

我正在尝试在我的网络应用程序中集成G​​oogle Calendar API以获得“检查可用性”功能。目前我正处于挂钩的基础之中,而我所获得的最远的只是尝试进行身份验证并提取一些数据。我已经在下面包含了我的代码 - 按照我看到的所有建议后,我得到的全部是403 Forbidden错误。

谁能发现我哪里错了?

public void Page_Load(object sender, EventArgs e)
{
  // Register the authenticator. The Client ID and secret have to be copied from the API Access
  // tab on the Google APIs Console.
  var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description);
  provider.ClientIdentifier = "MY_CLIENT_ID";
  provider.ClientSecret = "MY_SECRET";

  var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthentication);

  // Create the service.
  var service = new CalendarService(new BaseClientService.Initializer
  {
      Authenticator = auth
  });

  EventsResource.ListRequest req = service.Events.List("primary");

  if (req != null)
  {
      var events = req.Fetch();

      if (events != null)
      {
          litResult.Text = events.Items.Count().ToString();
      }
      else
      {
          litResult.Text = "Zilch.";
      }
  }
  else
  {
      litResult.Text = "Nada.";
  }
}

private static IAuthorizationState GetAuthentication(NativeApplicationClient arg)
{
  // Get the auth URL:
  IAuthorizationState state = new AuthorizationState(new[] { "https://www.google.com/calendar/feeds" });
  state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
  Uri authUri = arg.RequestUserAuthorization(state);

  // Request authorization from the user (by opening a browser window):
  Process.Start(authUri.ToString());
  Console.Write("  Authorization Code: ");
  string authCode = Console.ReadLine();
  Console.WriteLine();

  // Retrieve the access token by using the authorization code:
  return arg.ProcessUserAuthorization(authCode, state);
}
c# .net google-calendar-api
1个回答
0
投票

我想你在哪里:

provider.ClientIdentifier = "MY_CLIENT_ID";
provider.ClientSecret = "MY_SECRET";

你真的需要填写API Console页面上给出的客户端ID和密码...

© www.soinside.com 2019 - 2024. All rights reserved.