シリコンバレーのマーケター日記

東京、シンガポールを経てシリコンバレーで働く、マーケター。英語、プログラミング、データ分析は次世代マーケターの必須スキルだと信じて進む。

Facebook API 入門

Facebook APIの規約について質問を受けました。海外のプラットフォームの場合、開発者向けサイトは翻訳されていなく、そしてだいぶ曖昧な表現が多く読んでもイマイチわからない。というケースがよくあります。

そんなときは、自分で使ってみるのが一番早い。とはいえ、developer登録してaccess tokenとapp IDを発行して、、と手間がかかります。というわけで、プラットフォームが用意している以下のようなAPIお試しツールを使いましょう。

Facebook Graph API Explorer: https://developers.facebook.com/tools/explorer

Twitter API Concole Tool: API Console Tool | Twitter Developers

 

例:アプリがFacebookの友達リストを入手するための必要条件は?

Step.1: 開発者ページからHTTP APIリクエストのコードを入手

Facebook開発者ページから、friend情報に関するAPIのページをさがします。どうやら、"API用URL/{user id}/friends" と入れると、friend listが返ってくるようです。

f:id:yuyutata:20160504174143p:plain

Step.2: 上記のFacebook Graph API Explorerに行って、実際にsubmitする

friend listが出てきません。あれれ。

f:id:yuyutata:20160504174713p:plain

Step.3: Step.1とStep.2を繰り返す。

ページに戻ると以下のように書いてあります。

- A user access token with user_friends permission is required to view the current person's friends.

- This will only return any friends who have used (via Facebook Login) the app making the request.

- If a friend of the person declines the user_friends permission, that friend will not show up in the friend list for this person.

なるほど。user_friends permission が必要なようです。というわけで、Graph API Explorerに戻って、”Get Token”をクリックして、”Get User Access Token”をクリックします。指示通り、user_friendsを選んで”Get Access Token"!

f:id:yuyutata:20160504181920p:plain

そして、me/friendsと打って"submit"とすると、自分の友達リストが出てきたと思います。しかし、以下のdebugメッセージも出てきました。

f:id:yuyutata:20160504233958p:plain

  • Only friends who installed this app are returned in API v2.0 and higher. total_count in summary represents the total number of friends, including those who haven't installed the app.Learn More
  • Friend list is no longer part of the default permission set and has its own permission: Asking for access to a person's friend list is now a separate permission that your app must request. The new permission is called user_friends.
  • Friend list now only returns friends who also use your app: The list of friends returned via the/me/friends endpoint is now limited to the list of friends that have authorized your app. 

 結論:API v2.0以上では、アプリにuser_friends permissionを付与したユーザーしかfriend listには引っかからない(つまり全ての人がそのアプリにFacebookログインして、user_friends permissionを付与しなければ、全ての友達関係は入手できない)。

試しに、実際のテストアプリ(友達だれもログインしてない)でme/friendsをsubmitしてみると、以下のようになりました。

Tips: 右上のApplicationのドロップダウンメニューでGraph API Expolerから自分でdeveloper登録したアプリに切り替える。

f:id:yuyutata:20160504234930p:plain

f:id:yuyutata:20160504234616p:plain

 

Facebook APIのまとめ

  • 困ったらdeveloper pageでAPIを確認
  • Facebook Graph API ExplorerでAPIの挙動を確認
  • 実際のアプリでの挙動を確認したい場合は、実際に登録したアプリに切り替えてAPI Explorerで確認

 

FBアクセストークン確認ツール: 

https://developers.facebook.com/tools/debug/

 

こんな感じカニ。