参考にさせて頂きました!
Google Calendar API v3 でカレンダーに予定を登録する
(事前準備としてプロジェクトを登録は、上記を参照)
Google APIs Client Library for .NET が必要!
Google Calendar API v3 OAuth2.0のサンプル
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; ... 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 = "YOUR_CLIENT_ID"; provider.ClientSecret = "YOUR_CLIENT_SECRET"; var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization); // Create the service. This will automatically call the previously registered authenticator. var service = new CalendarService(auth); ... } 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); }
public static void Main(string[] args) 内 GetAuthorization がありません!
var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization);
いきなりエラー!
private static IAuthorizationState GetAuthentication(NativeApplicationClient arg)
GetAuthentication を GetAuthorization に変更
private static IAuthorizationState GetAuthorization(NativeApplicationClient arg)
コンソールが表示されるだけで何も起こりません!? ****.Fetch(); フェッチしないといけないようです。なので、以下を追加。
Calendar calendar = service.Calendars.Get("primary").Fetch();
Console.WriteLine(calendar.Summary);
Console.ReadKey();
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;
...
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 = "YOUR_CLIENT_ID";
provider.ClientSecret = "YOUR_CLIENT_SECRET";
var auth = new OAuth2Authenticator(provider, GetAuthorization);
// Create the service. This will automatically call the previously registered authenticator.
var service = new CalendarService(auth);
Calendar calendar = service.Calendars.Get("primary").Fetch();
Console.WriteLine(calendar.Summary);
Console.ReadKey();
}
認証画面が出ました!
[アクセス許可]をクリックすると認証コードが表示されます。
カレンダー名が表示されます。
しかし、この方法だと毎回「アクセス許可」をしないといけません!
ググっても情報が少ない!!!
RefreshToken を行えばOK!(これがわかるまでに相当な時間が...!)
"RefreshToken Code"は、最初の[アクセス許可](arg.ProcessUserAuthorization)を行なった時に取得できます。
※再度[アクセス許可](arg.ProcessUserAuthorization)を行うと"RefreshToken Code"は、変わってしまいます。
private static IAuthorizationState GetAuthorization(NativeApplicationClient arg)
{
IAuthorizationState state = new AuthorizationState(new[] { CalendarService.Scopes.Calendar.GetStringValue() });
state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
//本来ならAccessTokenの期限切れならRefreshTokenを行う処理にしたいが、また今度!
//https://www.googleapis.com/oauth2/v1/tokeninfo?access_token="AccessToken"
state.RefreshToken = "RefreshToken Code";
bool b = arg.RefreshToken(state);
//成功したら true がかえってくる。
return state;
}
「GCalenMaker:ファイルメーカー用プラグイン(Windows専用)
Googleカレンダーへイベントを追加,編集,削除。Googleカレンダーからイベントを読込。」
は、Google Calendar API v2を利用しているのでV3仕様に変更しないといけない...。
0 件のコメント:
コメントを投稿