前提・実現したいこと
FlutterでTwitter API v2 を用いて,ツイートを行いたいと考えています。
Timelineの取得など,GETでのリクエストは以下のように,正しく値を取得できているのですが、POSTでのリクエストは、「発生している問題・エラーメッセージ」に記載のように403が返ってきてしまいます。
ソースコードの処理の流れとしては、
- 別のファイルで事前に取得しFirebaseに保存している、access_tokenと、access_token_secretを読み込む。
- oauth1を使ってツイートをするためのエンドポイントにリクエストをPOST形式で送信。
という流れです。
GETでのリクエスト
1flutter: [{created_at: Fri Jan 21 13:06:55 +0000 2022, id: 1484512834734424067, id_str: 1484512834734424067, text: 負け癖とか逃げ癖ってなんなんでしょ。 2ユニコーンとかケルベロスみたいなやつ?, truncated: false, entities: {hashtags: [], symbols: [], user_mentions: [], urls: []}, source: <a href="http://twitter.com/download/iphone" rel="nofollow">Twitter for iPhone</a>, in_reply_to_status_id: null, in_reply_to_status_id_str: null, in_reply_to_user_id: null, in_reply_to_user_id_str: null, in_reply_to_screen_name: null, user: {id: 746004913658822657, id_str: 746004913658822657, name: ダイキ@筋トレせよお兄さん🏋🏻♂️, screen_name: daiki_kintore, location: 山形⇒京都⇒東京⇒山形⇒大阪(2月〜), description: □座右の銘:ストイックにカッコよく!! ■同志社大卒🎓■元レスリング🤼ユース日本2位・アジア大会出場🇯🇵■大学:学生団体設立幹部→広告代理店営業→人材営業<…>
発生している問題・エラーメッセージ
flutter: {title: Forbidden, detail: Forbidden, type: about:blank, status: 403}
該当のソースコード
import 'dart:convert'; import 'dart:developer'; import 'package:flutter/material.dart'; import 'package:oauth1/oauth1.dart' as oauth1; import 'package:url_launcher/url_launcher.dart'; import '../header.dart'; import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:firebase_core/firebase_core.dart'; import 'package:http/http.dart' as http; class Home extends StatefulWidget { @override _MyWidgetState createState() => _MyWidgetState(); } class _MyWidgetState extends State<Home> { final controller = TextEditingController(); final platform = oauth1.Platform( 'https://api.twitter.com/oauth/request_token', 'https://api.twitter.com/oauth/authorize', 'https://api.twitter.com/oauth/access_token', oauth1.SignatureMethods.hmacSha1, ); final clientCredentials = oauth1.ClientCredentials( '<APIキー>', '<APIシークレットキー>', ); late final auth = oauth1.Authorization(clientCredentials, platform); oauth1.Credentials? tokenCredentials; @override Widget build(BuildContext context) { String access_token = 'access_token'; String access_token_secret = 'access_token_secret'; FirebaseFirestore.instance.collection('test_collection1').get().then((QuerySnapshot snapshot) { snapshot.docs.forEach((doc) { /// usersコレクションのドキュメントIDを取得する print(doc.id); /// 取得したドキュメントIDのフィールド値nameの値を取得する access_token = doc.get('access_token'); access_token_secret = doc.get('access_token_secret'); print(access_token); print(access_token_secret); }); }); final callbackUrl = "api.twitter.com/2/tweets"; return MaterialApp( home: Scaffold( appBar: Header(), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ // ログイン後に表示されたPINを入力 TextFormField( controller: controller, ), ElevatedButton( onPressed: () async { final client = oauth1.Client( platform.signatureMethod, clientCredentials, oauth1.Credentials( access_token, access_token_secret, ), ); final res = await client.post( Uri.https( 'api.twitter.com', '/2/tweets', {'text': 'テスト'}, ), ); final body = jsonDecode(res.body); print(body); }, child: Text('ホーム'), ), ], ), ), ), ); } }
試したこと
こちらに記載されているソースコードのgetをpostに変更したものでもリクエストを送ってみたが、以下のような返答が返ってきた。(OAuth 2.0では認証してくれないらしい。)
flutter: { "title": "Unsupported Authentication", "detail": "Authenticating with OAuth 2.0 Application-Only is forbidden for this endpoint. Supported authentication types are [OAuth 1.0a User Context, OAuth 2.0 User Context].", "type": "https://api.twitter.com/2/problems/unsupported-authentication", "status": 403 }
補足情報(FW/ツールのバージョンなど)
またv2になってから、dartのtwitter APIなどが非対応になっているそうで困っております。
その他必要な情報があれば、教えてください。

あなたの回答
tips
プレビュー