简体   繁体   English

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

[英]Google Calendar API and C# - 403 Forbidden

Looking for some help. 寻求一些帮助。 I've been through all the documentation, SO and Google and still having no luck. 我已经阅读了所有文档,SO和Google,但仍然没有运气。

I'm trying to integrate with the Google Calendar API in my Web Application for a "Check Availability" feature. 我正在尝试在我的网络应用程序中集成G​​oogle Calendar API以获得“检查可用性”功能。 Currently I'm at the very basics of hooking in, and the furthest I've got is just trying to authenticate and pull some data out. 目前我正处于挂钩的基础之中,而我所获得的最远的只是尝试进行身份验证并提取一些数据。 I've included my code below - all I'm getting are 403 Forbidden errors after following all the advice I've seen. 我已经在下面包含了我的代码 - 按照我看到的所有建议后,我得到的全部是403 Forbidden错误。

Can anyone spot where I am going wrong? 谁能发现我哪里错了?

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);
}

I think where you have: 我想你在哪里:

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

You really need to fill it in with the client id and secret given on the API Console page... 您真的需要使用API控制台页面上提供的客户端ID和密码填写它...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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