22
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Google Apps ScriptでTwitter APIを叩く

Posted at

やりたいこと

  • gasからTwitterAPIを叩きたい
  • access token, access token secretを持っているのに、それを使わずにoauth認証するような実装をしなきゃいけないのが面倒
  • 直接これらを渡して、直接API叩く方法無いのかな?
  • あった!

どうやったか

gasのOAuth1ライブラリにsetAccessTokenというメソッドが用意したあったのでそれを使えばOKでした
サンプルとかドキュメントにはこのメソッド出てこないが、ライブラリのソースコード読んでたら見つけました


var getTwitterService = function () {
  return OAuth1.createService("Twitter")
    .setAccessTokenUrl("https://api.twitter.com/oauth/access_token")
    .setRequestTokenUrl("https://api.twitter.com/oauth/request_token")
    .setAuthorizationUrl("https://api.twitter.com/oauth/authorize")
    .setConsumerKey("xxx")
    .setConsumerSecret("xxx")
    .setAccessToken("access token", "access token secret"); // これだよ!!
};

var service = getTwitterService();
var res = service.fetch("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=qiita");

事前にOAuth1ライブラリをgasに導入しておく必要あり → https://github.com/gsuitedevs/apps-script-oauth1#setup

参考

22
23
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?