この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
はじめに
AWSチームの鈴木です。
Facebookが提供するv1.0のAPI、2015年にサポートが終了し、 2016年8月、v1.0のAPIを利用したFacebookシェア数の取得が不能になりました。
v2.1のAPIの利用方法と、その実行に必要なアクセストークンの作成方法について 確認する機会がありましたので、紹介させていただきます。
Facebook 新旧APIの実行サンプル
- Facebookのシェア数を取得しています
- 対象URLは「https://dev.classmethod.jp/」
v1.0
- サポート期限、2015年4月30日まで。
- 2016年8月現在、v2.1 以降のAPI利用が求められる様になりました。
2016年8月以降
$ curl http://api.facebook.com/method/fql.query?format=json&query=select%20like_count,%20share_count%20from%20link_stat%20where%20url=%22https://dev.classmethod.jp/%22 > /dev/null
{
"error_code": 12,
"error_msg": "REST API is deprecated for versions v2.1 and higher (12)",
"request_args": [
{
"key": "method",
"value": "fql.query"
},
{
"key": "format",
"value": "json"
}
]
}
2016年8月以前
[
{
like_count: 0,
share_count: 637
}
]
v2.0
- 2016年8月現在、HTTPでトークンなしでの利用が可能、実行結果はv2.7と同一の結果が得られます。
- v2.0のサポートは2016年8月7日までとされています
$ curl http://graph.facebook.com/v2.0/?id=https://dev.classmethod.jp/
{
"og_object": {
"id": "xxxxxxxxxxxxxxxxx",
"description": "AWS/iOS技術者の必読メディア:クラスメソッド株式会社ブログ",
"title": "Developers.IO",
"type": "website",
"updated_time": "2016-07-25T15:15:07+0000"
},
"share": {
"comment_count": 0,
"share_count": 637
},
"id": "https://dev.classmethod.jp/"
}
v2.1〜2.7
- v2.1 以降のAPI利用には、アクセストークンが必須となりました
- トークン情報を保護するため、HTTPSの利用が必須となりました。
- 2016年8月現在、最新のAPIバージョンはV2.7となります。
$ curl "https://graph.facebook.com/v2.7/?access_token=********&id=https://dev.classmethod.jp/"
{
"og_object": {
"id": "xxxxxxxxxxxxxxxxx",
"description": "AWS/iOS技術者の必読メディア:クラスメソッド株式会社ブログ",
"title": "Developers.IO",
"type": "website",
"updated_time": "2016-07-25T15:15:07+0000"
},
"share": {
"comment_count": 0,
"share_count": 637
},
"id": "https://dev.classmethod.jp/"
}
Facebook トークン入手
Facebook API V2.1以降の利用に必要となるトークンは3種類、存在します。
- ユーザーアクセストークン
- アプリアクセストークン
- ページアクセストークン
Facebookシェア数の取得に利用したAPIは、アプリアクセストークンでの利用が可能でした。
アプリアクセストークンは、利用可能なAPIがFacebookアプリの設定変更などに限定されますが、ユーザアクセストークン、ページアクセストークンと比較して簡単な手順の発行が可能です。
また、ユーザアクセストークン、ページアクセストークンは60日以上有効なトークンを発行する事ができませんが、 アプリアクセストークンはFacebookアプリの削除や「app secret」を変更しない限り、発行したトークンを継続的に利用することが可能です。
アプリ作成
facebook for developersを利用して、Facebookアプリを作成します。
参考リンク
Facebookアプリの作成は、下記ブログで「クライアントの登録」として紹介されています
アプリID、app secret の確認
作成したアプリの「App ID」、「app secret」の値を確認します。 「app secret」の取り扱いにはご注意ください。
アクセストークンの動作確認
「App ID」と「app secret」は、「|」(パイプ)で結合させる事で、 アプリアクセストークンとして利用することも可能です。
シェア数取得APIの動作を確認します
AP_ID='000000000000000'
AP_SECRET='################################'
AP_TOKEN="${AP_ID}|${AP_SECRET}"
#echo ${AP_TOKEN}
get_id (){
curl -X GET "https://graph.facebook.com/v2.1/?id=https://dev.classmethod.jp/&access_token=${AP_TOKEN}"
}
get_id | jq .
{
"og_object": {
"id": "xxxxxxxxxxxxxxxxx",
"description": "AWS/iOS技術者の必読メディア:クラスメソッド株式会社ブログ",
"title": "Developers.IO",
"type": "website",
"updated_time": "2016-07-25T15:15:07+0000"
},
"share": {
"comment_count": 0,
"share_count": 637
},
"id": "https://dev.classmethod.jp/"
}
アプリアクセストークン の発行
アプリアクセストークンを発行する事で、「app secret」なしでAPIを実行する事も可能です。
AP_ID='000000000000000'
AP_SECRET='################################'
get_ap_token (){
curl -q -X GET "https://graph.facebook.com/oauth/access_token?client_id=${AP_ID}&client_secret=${AP_SECRET}&grant_type=client_credentials" | cut -f 2 -d =
}
get_ap_token
AP_TOKEN=`get_ap_token`
#echo ${AP_TOKEN}
get_id (){
curl -X GET "https://graph.facebook.com/v2.1/?id=https://dev.classmethod.jp/&access_token=${AP_TOKEN}"
}
get_id | jq .
{
"og_object": {
"id": "xxxxxxxxxxxxxxxxx",
"description": "AWS/iOS技術者の必読メディア:クラスメソッド株式会社ブログ",
"title": "Developers.IO",
"type": "website",
"updated_time": "2016-07-25T15:15:07+0000"
},
"share": {
"comment_count": 0,
"share_count": 637
},
"id": "https://dev.classmethod.jp/"
}
まとめ
セキュリティ確保のため、認証必須となったFacebook APIですが、シェア数の参照は アプリアクセストークンを利用することで簡単に実施出来ることが確認できました。
Facebook API、アプリバージョンパスを指定しない場合、最も古い有効バージョンが利用される仕様です。 現在v2.0のAPIが利用できる模様ですが、v2.0の提供終了に備えアップデートの準備をご検討ください。
参考情報
Facebook APIのバージョンと利用期限
バージョン | パス | リリース日 | 使用期限 |
---|---|---|---|
v1.0 | /{object} | 2010年4月21日 | 2015年4月30日以降は利用不可 |
v2.0 | /v2.0/{object} | 2014年4月30日 | 2016年8月7日 |
v2.1 | /v2.1/{object} | 2014年8月7日 | 2016年10月30日 |
v2.2 | /v2.2/{object} | 2014年10月30日 | 2017年3月25日 |
v2.3 | /v2.3/{object} | 2015年3月25日 | 2017年7月8日 |
v2.4 | /v2.4/{object} | 2015年7月8日 | 2017年10月7日 |
v2.5 | /v2.5/{object} | 2015年10月7日 | 2018年4月12日 |
v2.6 | /v2.6/{object} | 2016年4月12日 | 2018年7月13日 |
v2.7 | /v2.7/{object} | 2016年7月13日 | 2018年7月(延長の可能性あり) |