3

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

我正在尝试与我的 Web 应用程序中的 Google 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);
}
4

1 回答 1

0

我想你在哪里:

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

您确实需要使用API 控制台页面上提供的客户端 ID 和密码填写它...

于 2013-06-06T02:04:46.347 回答