我在c#.net中尝试了谷歌的这个示例,但它对我不起作用。任何人都有谷歌日历api v3的样本
它向我显示错误,因为当前内容中不存在AuthenticatorFactory。
我使用谷歌提供的dll
using System;
using System.Diagnostics;
using DotNetOpenAuth.OAuth2;
using Google.Apis.Authentication;
using Google.Apis.Authentication.OAuth2;
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
using Google.Apis.Calendar.v3;
using Google.Apis.Calendar.v3.Data;
using Google.Apis.Util;
namespace Sample.Console
{
class Program
{
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 = "YOUR_CLIENT_ID";
provider.ClientSecret = "YOUR_CLIENT_SECRET";
AuthenticatorFactory.GetInstance().RegisterAuthenticator(
() => new OAuth2Authenticator(provider, GetAuthentication));
// Create the service. This will automatically call the previously registered authenticator.
var service = new CalendarService();
}
private static IAuthorizationState GetAuthentication(NativeApplicationClient arg)
{
// Get the auth URL:
IAuthorizationState state = new AuthorizationState(new[] { CalendarService.Scopes.Calendar.GetStringValue() });
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);
}
}
}
答案 0 :(得分:0)
而不是使用Auth Factory使用OAuth2身份验证器对象,google没有很好地记录此更改...只需使用AuthenticatorFactory.GetInstance()....删除代码并替换为此代码行。其他一切都应该保持不变。
var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization);