我使用 Google.GData.Client 在 .NET 中执行此操作。一旦我完成了授权过程并保存了令牌,下次我的用户访问该站点时,我会通过生成一个 GOAuthRequestFactory 对象来获取授权。
public GOAuthRequestFactory GetGoogleOAuthFactory(int id)
{
// build the base parameters
OAuthParameters parameters = new OAuthParameters
{
ConsumerKey = kConsumerKey,
ConsumerSecret = kConsumerSecret
};
// check to see if we have saved tokens and set
var tokens = (from a in context.GO_GoogleAuthorizeTokens where a.id = id select a);
if (tokens.Count() > 0)
{
GO_GoogleAuthorizeToken token = tokens.First();
parameters.Token = token.Token;
parameters.TokenSecret = token.TokenSecret;
}
// now build the factory
return new GOAuthRequestFactory("somevalue", kApplicationName, parameters);
}
拥有请求工厂后,我可以调用我有权使用的各种 api 之一并执行以下操作:
// authenticate to the google calendar
CalendarService service = new CalendarService(kApplicationName);
service.RequestFactory = GetGoogleOAuthFactory([user id]);
// add from google doc record
EventEntry entry = new EventEntry();
entry.Title.Text = goEvent.Title;
entry.Content.Content = GoogleCalendarEventDescription(goEvent);
When eventTime = new When(goEvent.StartTime, goEvent.EndTime.HasValue ? goEvent.EndTime.Value : DateTime.MinValue, goEvent.AllDay);
entry.Times.Add(eventTime);
// add the location
Where eventLocation = new Where();
eventLocation.ValueString = String.Format("{0}, {1}, {2} {3}", goEvent.Address, goEvent.City, goEvent.State, goEvent.Zip);
entry.Locations.Add(eventLocation);
Uri postUri = new Uri(kCalendarURL);
// set the request and receive the response
EventEntry insertedEntry = service.Insert(postUri, entry);