DropboxのWebhookのurlをGoogle Cloud Functionsで作ったapiを使ってやってみる
- Data
- 2018/11/19
- Tag
Google Cloud Functions
簡単に言うとGCP上で Javascriptで書いた関数を実行するサービスです。Nodeを使った書くことができます。
詳しくは下記のサイトなどをみてください。
【GCP入門編・第14回】 Cloud Functions を使ってサーバレスアーキテクチャを体験しよう!
準備
Serverless Frameworkを使う
Serverless Frameworkを使うと簡単な感じでcloud functionsを使うことができるみたいで便利!
Google Cloud Platform のサービスアカウントキーを作成しておこう
下記サイトをみて手順通りに勧めていけばアカウントキーを作成できると思います。
Serverless Framework注意点
serverless.yml
を用意する際に、特にこの部分を忘れないでください!
Save the keyfile somewhere secure. We recommend making a folder in your root folder and putting it there. Like this, ~/.gcloud/keyfile.json. You can change the file name from keyfile to anything. Remember the path you saved it to.
保存したkeyfileを自分のローカルの任意の場所(ルートディレクトリなど)に置くことを推奨しています。
~/.gcloud/keyfile.json
のように配置して、serverless.yml内の
credentials: ~/.gcloud/keyfile.json`と記載しましょう。
Google Cloud Deployment Manager V2 APIは有効にしておきましょう。
serverless deploy
で怒られたらエラーに書いてあるアドレスにとんでapiを有効にしましょう。
Error: Access Not Configured. Google Cloud Deployment Manager API has not been used in project ******* before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/deploymentmanager.googleapis.com/overview?project=******* then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.
DropboxのWebhookについて
公式の説明: ttps://www.dropbox.com/developers/reference/webhooks
個々のアプリを指定してwebhookを設定しよう https://www.dropbox.com/developers/apps
challengeをsendでそのまま返す
返してあげないと認証うまく行ってってことでエラーになるためchallengeをそのままsendでお繰り返してあげるとうまくいきます。
例
export function test(req, res) {
res.status(200).send(req.query.challenge);
}
req.body
には下記のようにデータが入っています
{
"list_folder":{
"accounts":[
"dbid:AAH4f99T0taONIb-OurWxbNQ6ywGRopQngc"、
...
]
}、
"delta":{
"ユーザー":[
12345678、
23456789、
...
]
}
}
参考
少し調べたのをまとめました。
他にも色々調べたり実際にものができたら共有予定です。
実際にDropbox PaperのWebhookを使いたかったのですが。
Paperには対応していないのですかね。。