Flutter (Dart 2) accessing twitter GET api

橙三吉。 提交于 2019-12-06 04:24:05

Here is the code that ended up working:

Generate string method:

static String generateSignature(
      String method, String base, List<String> sortedItems) {
    String param = '';

    for (int i = 0; i < sortedItems.length; i++) {
      if (i == 0)
        param = sortedItems[i];
      else
        param += '&${sortedItems[i]}';
    }

    String sig =
        '$method&${Uri.encodeComponent(base)}&${Uri.encodeComponent(param)}';
    String key =
        '${Uri.encodeComponent(_secretKey)}&${Uri.encodeComponent(twitter.secret)}';
    var digest = Hmac(sha1, utf8.encode(key)).convert(utf8.encode(sig));
    return base64.encode(digest.bytes);
  }

Convenience method for twitter get calls:

Future<http.Response> _twitterGet(
      String base, List<List<String>> params) async {
    if (twitter == null) await _startSession();

    String oauthConsumer =
        'oauth_consumer_key="${Uri.encodeComponent(_consumerKey)}"';
    String oauthToken = 'oauth_token="${Uri.encodeComponent(twitter.token)}"';
    String oauthNonce =
        'oauth_nonce="${Uri.encodeComponent(randomAlphaNumeric(42))}"';
    String oauthVersion = 'oauth_version="${Uri.encodeComponent("1.0")}"';
    String oauthTime =
        'oauth_timestamp="${(DateTime.now().millisecondsSinceEpoch / 1000).toString()}"';
    String oauthMethod =
        'oauth_signature_method="${Uri.encodeComponent("HMAC-SHA1")}"';
    var oauthList = [
      oauthConsumer.replaceAll('"', ""),
      oauthNonce.replaceAll('"', ""),
      oauthMethod.replaceAll('"', ""),
      oauthTime.replaceAll('"', ""),
      oauthToken.replaceAll('"', ""),
      oauthVersion.replaceAll('"', "")
    ];
    var paramMap = Map<String, String>();

    for (List<String> param in params) {
      oauthList.add(
          '${Uri.encodeComponent(param[0])}=${Uri.encodeComponent(param[1])}');
      paramMap[param[0]] = param[1];
    }

    oauthList.sort();
    String oauthSig =
        'oauth_signature="${Uri.encodeComponent(generateSignature("GET", "https://api.twitter.com$base", oauthList))}"';

    return await http
        .get(new Uri.https("api.twitter.com", base, paramMap), headers: {
      "Authorization":
          'Oauth $oauthConsumer, $oauthNonce, $oauthSig, $oauthMethod, $oauthTime, $oauthToken, $oauthVersion',
      "Content-Type": "application/json"
    }).timeout(Duration(seconds: 15));
  }

Example call:

Future<User> getUser(String tag) async {
    String base = '/1.1/users/show.json';
    final response = await _twitterGet(base, [
      ["screen_name", tag],
      ["tweet_mode", "extended"]
    ]);

    if (response.statusCode == 200) {
      try {
        return User(json.decode(response.body));
      } catch (e) {
        print(e);
        return null;
      }
    } else {
      print("Error retrieving user");
      print(response.body);
      return null;
    }
  }

Here is the doc for twitter user authentication: https://developer.twitter.com/en/docs/basics/authentication/overview/3-legged-oauth

The first 3 steps are handled by flutter_twitter_login. Take a look at the last example:

Step 4: Using these credentials for app-user required requests

Example POST statuses/update

Request includes:

POST statuses/update.json

oauth_consumer_key=cChZNFj6T5R0TigYB9yd1w

oauth_token=7588892-kagSNqWge8gB1WwE3plnFsJHAZVfxWD7Vb57p0b4

Another example can be found here: https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/post-statuses-update

$ curl --request POST 
--url 'https://api.twitter.com/1.1/statuses/update.json?
status=Test%20tweet%20using%20the%20POST%20statuses%2Fupdate%20endpoint' 
--header 'authorization: OAuth oauth_consumer_key="YOUR_CONSUMER_KEY",
oauth_nonce="AUTO_GENERATED_NONCE", oauth_signature="AUTO_GENERATED_SIGNATURE",
oauth_signature_method="HMAC-SHA1", oauth_timestamp="AUTO_GENERATED_TIMESTAMP",
oauth_token="USERS_ACCESS_TOKEN", oauth_version="1.0"' 
--header 'content-type: application/json'

I'm not sure if you need all of these arguments, but the header for a simple get request probably needs at least this:

headers: {
  "Authorization": 'oauth_consumer_key="${consumerKey}", oauth_token="${twitter.token}"', 
  "Content-Type": "application/json"
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!