まずは新規のスクリプトファイルを作成します
Standalone Scriptsを作成する方法
ファイルを以下のコード.gsで置き換えます
コード.gsvar BASE_URL = 'https://graph.facebook.com/';
/************************************
ファイル > プロジェクトのプロパティ > スクリプトのプロパティに保存したアクセストークンを取得して返す
************************************/
function getToken() {
return PropertiesService.getScriptProperties().getProperty('api_token');
}
/************************************
groupIdを指定してfeedを取得する
************************************/
function getFeed(){
var groupId = '208177xxxxxxxxxxxx';
var url = BASE_URL + groupId + '/feed';
var jobj = getData(url);
Logger.log(jobj);
var data = jobj['data'][0];
var message = data['message'];
Logger.log(message);
}
/************************************
postId(投稿のID)を指定してコメントを取得する
************************************/
function getComments(){
var postId = '';
var url = BASE_URL + postId + '/comments';
var jobj = getData(url);
Logger.log(jobj);
}
/************************************
dataを取得して返す
************************************/
function getData(url) {
var headers = getHeaders();
var options = getOptions(headers);
var response = UrlFetchApp.fetch(url, options).getContentText();
var jobj = JSON.parse(response);
return jobj;
}
/************************************
headersを作って返す
************************************/
function getHeaders(){
var headers = {
"Authorization": 'Bearer ' + getToken()
}
return headers;
}
/************************************
optionsを作って返す
************************************/
function getOptions(headers){
var options = {
"method": "get",
"contentType": "application/json",
"headers": headers,
"muteHttpExceptions": true
};
return options;
}
|
アクセストークンを保存します
WorkplaceのAPIのアクセストークンを取得するで取得して
ファイル > プロジェクトのプロパティの
スクリプトのプロパティにapi_token等の名前で値として保存します
「+行を追加」をクリックして
プロパティにapi_tokenと入力します
値に先程コピーしたアクセストークンを貼り付けます
グループIDを指定します
var groupId = '208177xxxxxxxxxxxx';には取得したいグループのIDを入力します。
WorkplaceのIntegrationsで「Grant Permissions」を設定します
「Select Groups」で対象のグループを設定して
「保存」をクリックします
実行する
Google Apps Scriptで
実行 > 関数を実行 > getFeed で実行します
承認が求められるので「許可を確認」をクリックします
自分のアカウントを選択します
内容を確認して「許可」をクリックします
実行結果
Logger.log(jobj);
で返ってくるデータをログに出しているので
表示 > ログ で見ることができます
今回のコードでは
var data = jobj['data'][0];
var message = data['message'];
Logger.log(message);
で先頭のmessageを取得しているので
指定したグループの最新の投稿のメッセージもログに出しています
参考
Graph API Reference
https://developers.facebook.com/docs/workplace/integrations/custom-integrations/reference/
facebook for developers
https://developers.facebook.com/docs/workplace?locale=ja_JP
Custom Integrations
https://developers.facebook.com/docs/workplace/custom-integrations-new
Graph API Reference
https://developers.facebook.com/docs/workplace/integrations/custom-integrations/reference/
facebook for developers
https://developers.facebook.com/docs/workplace?locale=ja_JP
Custom Integrations
https://developers.facebook.com/docs/workplace/custom-integrations-new









