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

时间:2013-05-23 17:12:14

标签: c# .net google-calendar-api

寻求一些帮助。我已经阅读了所有文档,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);
}

1 个答案:

答案 0 :(得分:0)

我想你在哪里:

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

您确实需要使用API Console页面上提供的客户ID和密码填写...