しっぽを追いかけて

ぐるぐるしながら考えています

Unity と猫の話題が中心   掲載内容は個人の私見であり、所属組織の見解ではありません

Xamarin.Forms で Instagram から投稿写真情報を取得する

認証に成功しのでさっそく InstagramAPI を Xamarin.From で利用してみます

Instagram の開発者向けサイトに用意されている下記の API コンソールで使い方を確認し

API Console • Instagram Developer Documentation

最近投稿した写真を取得する API users/{user-id}/media/recent というのが使えそうなので試します


/// <summary>
/// Page appearing event handler
/// </summary>
protected override void OnAppearing()
{
    base.OnAppearing();

    // Navigate to facebook authorize page
    this.webView.Source =
        string.Format(
            @"https://api.instagram.com/oauth/authorize/?client_id={0}&redirect_uri={1}&response_type=code",
            ClientId,
            WebUtility.UrlEncode(@"http://www.facebook.com/connect/login_success.html"));

    this.webView.Navigating += this.OnNavigating;
}

/// <summary>
/// WebView navigating event handler
/// </summary>
/// <param name="sender">event sender</param>
/// <param name="e">event arguments</param>
private async void OnNavigating(object sender, WebNavigatingEventArgs e)
{
    if (e.Url.StartsWith(@"http://www.facebook.com/connect/login_success.html"))
    {
        this.webView.Navigating -= this.OnNavigating;

        var uri = new Uri(e.Url);
        if (uri.Query.StartsWith("?code="))
        {
            var code = uri.Query.TrimStart('?').Split('&').First().Split('=').LastOrDefault();

            var client = new HttpClient();
            var content = new FormUrlEncodedContent(new Dictionary<string, string>
            {
                {"client_id", ClientId},
                {"client_secret", ClientSecret},
                {"grant_type", @"authorization_code" },
                {"redirect_uri", @"http://www.facebook.com/connect/login_success.html" },
                {"code", code },
            });

            var result = await client.PostAsync(@"https://api.instagram.com/oauth/access_token", content);
            var json = await result.Content.ReadAsStringAsync();
            var response = JsonConvert.DeserializeObject<Newtonsoft.Json.Linq.JContainer>(json);

            Debug.WriteLine(response);

            result = await client.GetAsync(string.Format(@"https://api.instagram.com/v1/users/{0}/media/recent?access_token={1}",
                                            response["user"]["id"],
                                            response["access_token"]));
            json = await result.Content.ReadAsStringAsync();
            response = JsonConvert.DeserializeObject<Newtonsoft.Json.Linq.JContainer>(json);

            await DisplayAlert("XamarinFacebook", string.Format("Instagram 写真情報取得に成功しました, response={0}", response), "OK");

            Debug.WriteLine(response);

            this.webView.Source = new Uri(response["data"][0]["images"]["standard_resolution"]["url"].ToString());
        }
    }
}

こんな感じで前回のコードを修正すればいけるはず・・・ということで実行

f:id:matatabi_ux:20150801151849g:plain

すんなり取れましたね

f:id:matatabi_ux:20150801152329p:plain

この API のレスポンス JSON はこんな感じでした・・・構造も素直でよろしい

残念ながら写真のアップロードは API ではサポートしないようです

あくまで情報収集のみですね