谷歌日历API和c# - 403禁止
本文关键字:禁止 日历 API 谷歌 | 更新日期: 2023-09-27 18:14:58
寻求帮助。我已经浏览了所有的文档,SO和Google,但仍然没有运气。
我试图与谷歌日历API集成在我的Web应用程序的"检查可用性"功能。目前我只掌握了最基本的连接,我所能做的就是验证并提取一些数据。我已经包括我的代码下面-所有我得到的是403禁止错误之后,我已经看到了所有的建议。
有谁能指出我哪里错了吗?
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);
}
我认为你有:
provider.ClientIdentifier = "MY_CLIENT_ID";
provider.ClientSecret = "MY_SECRET";
你真的需要用API控制台页面上给出的客户端id和secret来填写它…