谷歌API V3的dotnet的;使用日历API密钥 [英] Google API v3 for dotnet; using the calendar with an API key
问题描述
我想读我自己的谷歌日历与v3的API(HTTP://$c$c.google.com/p/google-api-dotnet-client/)。我最终想要的是显示我自己的日历从谷歌日历我的网站上通过一个自定义的控制(以及后来的让人们进行自定义约会)。每样我读的基础上,premise,我应该手动给访问我的数据。仍然在谷歌API控制台我创建了一个API密钥的应用程序。我试图创建我的code有效IAuthentication。从来没有少我不明白我必须要使用的验证。下面是一个可怜的尝试,谁可以帮助我?
公共无效TestMethod1()
{
// HTTP://$c$c.google.com/p/google-api-dotnet-client/wiki/OAuth2
NativeApplicationClient提供商= NativeApplicationClient(GoogleAuthenticationServer.Description);
NativeApplicationClient(GoogleAuthenticationServer.Description);
provider.ClientIdentifier =×;
provider.ClientSecret =×;
VAR AUTH =新OAuth2Authenticator< NativeApplicationClient>(供应商,GetAuthorization);
Google.Apis.Calendar.v3.CalendarService服务=新Google.Apis.Calendar.v3.CalendarService(AUTH);
变种结果= service.Events.List(主)取()。
Assert.IsTrue(result.Items.Count大于0);
}
私有静态IAuthorizationState GetAuthorization(NativeApplicationClient ARG)
{
IAuthorizationState状态=新AuthorizationState(新[] {CalendarService.Scopes.Calendar.GetStringValue()});
返回arg.ProcessUserAuthorization(状态);
}
我这工作:
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用Google.Apis.Calendar;
使用Google.Apis.Calendar.v3;
使用Google.Apis.Authentication;
使用Google.Apis.Authentication.OAuth2;
使用Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
使用DotNetOpenAuth.OAuth2;
使用System.Diagnostics程序;
使用Google.Apis.Calendar.v3.Data;
命名空间consoleGoogleResearch
{
类节目
{
公共静态无效的主要(字串[] args)
{
//注册认证。客户端ID和秘密都被从API访问复制
//对谷歌API控制台选项卡。
VAR提供商=新NativeApplicationClient(GoogleAuthenticationServer.Description);
provider.ClientIdentifier =fill_in_yours;
provider.ClientSecret =fill_in_yours;
//创建服务。这会自动调用认证。
VAR的服务=新CalendarService(新OAuth2Authenticator< NativeApplicationClient>(供应商,GetAuthentication));
Google.Apis.Calendar.v3.CalendarListResource.ListRequest clrq = service.CalendarList.List();
VAR的结果= clrq.Fetch();
如果(result.Error!= NULL)
{
Console.WriteLine(result.Error.Message);
Console.ReadKey();
返回;
}
Console.WriteLine(日历);
的foreach(CalendarListEntry日历result.Items)
{
Console.WriteLine({0},calendar.Id);
Console.WriteLine(\ tAppointments:);
Google.Apis.Calendar.v3.EventsResource.ListRequest ELR = service.Events.List(calendar.Id);
VAR事件= elr.Fetch();
的foreach(在events.Items事件E)
{
Console.WriteLine(\ t来源:{0}为:{1}描述:{2},位置:{3},e.Start,e.End,e.Description,e.Location);
}
}
Console.ReadKey();
}
私有静态IAuthorizationState GetAuthentication(NativeApplicationClient ARG)
{
//获取身份验证网址:
// IAuthorizationState状态=新AuthorizationState(新[] {CalendarService.Scopes.Calendar.ToString()});
IAuthorizationState状态=新AuthorizationState(新[] {https://www.google.com/calendar/feeds});
state.Callback =新的URI(NativeApplicationClient.OutOfBandCallbackUrl);
乌里authUri = arg.RequestUserAuthorization(州);
//请求从用户授权(通过打开浏览器窗口):
的Process.Start(authUri.ToString());
Console.Write(授权code:);
字符串AUTH code =到Console.ReadLine();
//使用授权code。获取访问令牌:
返回arg.ProcessUserAuthorization(AUTH code,状态);
}
}
}
I'm trying to read my own Google calendar with the v3 API (http://code.google.com/p/google-api-dotnet-client/). What I eventually want is to display my own calendar from Google Calendar on my site through a custom control (and later on let people make custom appointments). Every sample I'm reading is based on the premise that I should manually give access to my data. Still in the Google API console I created an application with a API key. I’m trying to create a valid IAuthentication for my code. Never the less I don’t get which authentication I have to use. Below is a miserable attempt, who can help me with this?
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);
}
Hi for me this works:
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);
}
}
}
这篇关于谷歌API V3的dotnet的;使用日历API密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!