gpt4 book ai didi

.net - 用于 dotnet 的 Google API v3;使用带有 API key 的日历

转载 作者:行者123 更新时间:2023-12-04 06:04:21 25 4
gpt4 key购买 nike

我正在尝试使用 v3 API (http://code.google.com/p/google-api-dotnet-client/) 读取我自己的 Google 日历。我最终想要的是通过自定义控件在我的网站上从 Google 日历显示我自己的日历(然后让人们进行自定义约会)。我正在阅读的每个样本都基于我应该手动访问我的数据的前提。仍然在 Google API 控制台中,我使用 API key 创建了一个应用程序。我正在尝试为我的代码创建一个有效的 IAuthentication。无论如何,我都无法获得必须使用的身份验证。下面是一个悲惨的尝试,谁能帮我解决这个问题?

  public void TestMethod1()
{
// http://code.google.com/p/google-api-dotnet-client/wiki/OAuth2
NativeApplicationClient provider = NativeApplicationClient(GoogleAuthenticationServer.Description);
NativeApplicationClient(GoogleAuthenticationServer.Description);

provider.ClientIdentifier = "x";
provider.ClientSecret = "x";

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


Google.Apis.Calendar.v3.CalendarService service = new Google.Apis.Calendar.v3.CalendarService(auth);
var result = service.Events.List("primary").Fetch();

Assert.IsTrue(result.Items.Count > 0);
}

private static IAuthorizationState GetAuthorization(NativeApplicationClient arg)
{
IAuthorizationState state = new AuthorizationState(new[] { CalendarService.Scopes.Calendar.GetStringValue() });

return arg.ProcessUserAuthorization("", state);
}

最佳答案

嗨对我来说这是有效的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Google.Apis.Calendar;
using Google.Apis.Calendar.v3;
using Google.Apis.Authentication;
using Google.Apis.Authentication.OAuth2;
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
using DotNetOpenAuth.OAuth2;
using System.Diagnostics;
using Google.Apis.Calendar.v3.Data;

namespace consoleGoogleResearch
{
class Program
{
public static void Main(string[] args)
{
// 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 = "fill_in_yours";
provider.ClientSecret = "fill_in_yours";
// Create the service. This will automatically call the authenticator.
var service = new CalendarService(new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthentication));

Google.Apis.Calendar.v3.CalendarListResource.ListRequest clrq = service.CalendarList.List();
var result = clrq.Fetch();

if (result.Error != null)
{
Console.WriteLine(result.Error.Message);
Console.ReadKey();
return;
}

Console.WriteLine("Calendars: ");
foreach (CalendarListEntry calendar in result.Items)
{
Console.WriteLine("{0}", calendar.Id);
Console.WriteLine("\tAppointments:");
Google.Apis.Calendar.v3.EventsResource.ListRequest elr = service.Events.List(calendar.Id);
var events = elr.Fetch();
foreach (Event e in events.Items)
{
Console.WriteLine("\t From: {0} To: {1} Description: {2}, Location: {3}", e.Start, e.End, e.Description, e.Location);
}
}
Console.ReadKey();
}

private static IAuthorizationState GetAuthentication(NativeApplicationClient arg)
{
// Get the auth URL:
//IAuthorizationState state = new AuthorizationState(new[] { CalendarService.Scopes.Calendar.ToString() });
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();

// Retrieve the access token by using the authorization code:
return arg.ProcessUserAuthorization(authCode, state);
}
}
}

关于.net - 用于 dotnet 的 Google API v3;使用带有 API key 的日历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8537681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com