IS12Tを手にいれたけど、何かアプリつくろう!って思って考えてたら
Google+のクライアントがないことに気づいたのでそれを作ってみようと思う。
サンプルを読み込む
とにかくOAuthアクセスを実現する必要があるはずなので
まずはサンプルを取得してきます。
https://developers.google.com/+/downloads
http://code.google.com/p/google-api-dotnet-client/
ここにサンプルとあるのでそれを取得。
※各種OAuthとかの文章ありますけど、ソースとかが古かったりですね。
「Tasks.SimpleOAuth2」というプロジェクトがコンソールでできるやつみたいなので
そちらをスタートプロジェクトにして実行するわけですけど、
「Google.Apis.Samples.TasksOAuth2.ClientCredentials」に
APIキーの設定のがあるの以下のように設定します。
- public static readonly string ClientID = "xxxxxxxx.apps.googleusercontent.com";
- /// <summary>
- /// The OAuth2.0 Client secret of your project.
- /// </summary>
- public static readonly string ClientSecret = "xxxxxxxxxxxxxxxxlOd2y";
- /// <summary>
- /// Your Api/Developer key.
- /// </summary>
- public static readonly string ApiKey = "urn:xxxxxxxxxx";
OAuth用の準備
https://code.google.com/apis/console
に行ってGoogle+APIをONにしておきます。
今は「Courtesy limit: 1,000 queries/day」なので
1日1000回のアクセスまでのようですね。。。。ん?ユーザじゃなくてAPIキーでかな?
API AccessのところにOAuth2.0があるのでそこでキーを発行します。
OAuthでお馴染みのClientIDとSecretですね。
Google PlusのAPIを使ってみる
このサンプルはTasks APIの
PlusのDLLはBinaryサンプルのServiceの中にあります。
これを参照設定して追加してPlusServiceを使えるようにしておきます。
- // Display the header and initialize the sample.
- CommandLine.EnableExceptionHandling();
- CommandLine.DisplayGoogleSampleHeader("Google+ API");
コンソールにGoogleのロゴを出す部分ですね。特に大事なコードではないです。
一応TaskになっていたのでGoogle+に変更。
- // Register the authenticator.
- var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description);
- provider.ClientIdentifier = ClientCredentials.ClientID;
- provider.ClientSecret = ClientCredentials.ClientSecret;
- var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization);
ここでClientIDとSecretを使ってURLを作成している処理です。
GetAuthrorizarionは、サンプルであるProgram.csにあります。
- private static IAuthorizationState GetAuthorization(NativeApplicationClient arg)
- {
- // Get the auth URL:
- IAuthorizationState state = new AuthorizationState(new[] { PlusService.Scopes.PlusMe.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);
- }
NativeApplicationClient を元にOAuthにアクセスするURLを作成していきます。
IAuthorizarionStateでTasksのGetStringValue()を読んでいるのでPlusServiceにしてあげます。
実行するとこんな感じです。
ReadLine()でOAuthの認証コードを待ち受けます。
※実際の実行タイミングはFetch()するまでアクセスはされません。
Process.Start()で作成したURLにアクセスしますのでこれと同時にブラウザが立ちあがってるはずです。
アクセスを許可するとキーが発行されるのでそれをコンソールにコピペして、Enterします。
タスクに対して
- var service = new PlusService(auth);
- Person me = service.People.Get("me").Fetch();
- CommandLine.WriteLine(" ^2" + me.Name);
- ActivitiesResource.Collection collection = new ActivitiesResource.Collection();
- ActivityFeed feed = service.Activities.List("me",collection).Fetch();
- foreach (Activity list in feed.Items)
- {
- CommandLine.WriteLine(" ^2" + list.Title);
- }
- CommandLine.PressAnyKeyToExit();
と行なってみたら、自分の投稿を取得できました。
はじめ自分のIDを指定して検索してたんですが
「うんなもんOAuthじゃねーヽ(`Д´)ノプンプン」だったんですけど"me"で取得できました。
一旦これでアクセス自体はできたみたいですね。
途中、OAuthとGoogle+のAPIと.NETのAPIを読んでいてパニック起こしました。
誰がどれを作っているのかわからなくなったからです。
上位のクラスとか結構しっかり作ってある感じなので
簡単にOAuth(Google+API)にアクセスすることができます。
これをネタにWindowsPhoneクライアントを作るっていう大きな作業が待ってます。
0 件のコメント:
コメントを投稿