Flutter (Dart 2) 访问推特 GET api
Flutter (Dart 2) accessing twitter GET api
我在 Flutter 应用程序中调用 Twitter api 时遇到问题。我已经使用 Twitter 登录库来获取我的令牌和秘密令牌,并且我有我的消费者和消费者秘密。但我无法正确形成 https 请求。我尝试过使用 Oauth2 客户端和直接请求,但都没有用。
我发现 this 带有 dart 1 Twitter 和 Oauth 实现的 repo,但我一直无法弄清楚如何将其转换为 Dart 2。非常感谢所有帮助。
编辑:这是最新的代码:
final response = await http.get(new Uri.https(
"api.twitter.com", "/1.1/statuses/home_timeline.json", {
"count": "200",
"tweet_mode": "extended",
"exclude_replies": "false"
}), headers: {
"Authorization": 'Bearer ${twitter.token}', //twitter.token is the token recieved from Twitter sign in process
"Content-Type": "application/json"
});
returns "errors":[{"code":89,"message":"Invalid or expired token."}]
我知道令牌有效
编辑 2:
Future<List<Tweet>> getTimeline() async {
print("Getting timeline");
var query = https.get(
"https://api.twitter.com/1.1/statuses/home_timeline.json?count=2&tweet_mode=extended&exclude_replies=false",
headers: {
"Authorization":
'oauth_consumer_key="$_consumerKey", oauth_token="${twitter.token}"',
"Content-Type": "application/json"
}).timeout(Duration(seconds: 15));
print("Before await");
final response = await query;
print("code: ${response.statusCode}");
...
}
经过更多调试后,twitter.token 可能出现空异常。修复后,我仍然收到错误的授权数据。我会继续尝试向 header 添加更多信息,看看是否有帮助。
编辑 3:
这是我生成签名的方法:
static String generateSignature(String method, String base, List<String> sortedItems) {
String sig = '$method&${Uri.encodeComponent(base)}&';
String param = '';
for (int i = 0; i < sortedItems.length; i++) {
if (i == 0)
param = sortedItems[i];
else
param += '&${sortedItems[i]}';
}
sig += Uri.encodeComponent(param);
String key = '${Uri.encodeComponent(_secretKey)}&${Uri.encodeComponent(twitter.secret)}';
var digest = Hmac(sha1, utf8.encode(key)).convert(utf8.encode(sig));
print("base: ${digest.bytes}");
print("sig: ${base64.encode(digest.bytes)}");
return base64.encode(digest.bytes);
}
时间线方法如下:
Future<List<Tweet>> getTimeline() async {
print("Getting timeline");
Future<http.Response> query;
try {
String base = 'https://api.twitter.com/1.1/statuses/home_timeline.json';
String count = 'count=2';
String mode = 'tweet_mode=extended';
String replies = 'exclude_replies=false';
String oauthConsumer = 'oauth_consumer_key="$_consumerKey"';
String oauthToken = 'oauth_token="${twitter.token}"';
String oauthNonce = 'oauth_nonce="${randomAlphaNumeric(20)}"';
String oauthVersion = 'oauth_version="1.0"';
String oauthTime =
'oauth_timestamp="${DateTime.now().millisecondsSinceEpoch}"';
String oauthMethod = 'oauth_signature_method="HMAC-SHA1"';
String oauthSig = 'oauth_signature="${generateSignature("GET", base, [
count,
replies,
oauthConsumer,
oauthNonce,
oauthTime,
oauthToken,
oauthVersion,
mode
])}"';
query = http.get(
new Uri.https("api.twitter.com", "/1.1/statuses/home_timeline.json", {
"count": "2",
"tweet_mode": "extended",
"exclude_replies": "false"
}),
headers: {
"Authorization": '$oauthConsumer, $oauthToken, $oauthVersion, $oauthTime, $oauthNonce, $oauthMethod, $oauthSig',
"Content-Type": "application/json"
}).timeout(Duration(seconds: 15));
} catch (e) {
print(e);
}
谢谢!
这是 Twitter 用户身份验证的文档:https://developer.twitter.com/en/docs/basics/authentication/overview/3-legged-oauth
前 3 个步骤由 flutter_twitter_login 处理。看看最后一个例子:
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
另一个例子可以在这里找到: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'
我不确定您是否需要所有这些参数,但是 header 对于一个简单的 get 请求可能至少需要这个:
headers: {
"Authorization": 'oauth_consumer_key="${consumerKey}", oauth_token="${twitter.token}"',
"Content-Type": "application/json"
}
这是最终运行的代码:
生成字符串方法:
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);
}
twitter get 调用的便捷方法:
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));
}
调用示例:
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;
}
}
我在 Flutter 应用程序中调用 Twitter api 时遇到问题。我已经使用 Twitter 登录库来获取我的令牌和秘密令牌,并且我有我的消费者和消费者秘密。但我无法正确形成 https 请求。我尝试过使用 Oauth2 客户端和直接请求,但都没有用。
我发现 this 带有 dart 1 Twitter 和 Oauth 实现的 repo,但我一直无法弄清楚如何将其转换为 Dart 2。非常感谢所有帮助。
编辑:这是最新的代码:
final response = await http.get(new Uri.https(
"api.twitter.com", "/1.1/statuses/home_timeline.json", {
"count": "200",
"tweet_mode": "extended",
"exclude_replies": "false"
}), headers: {
"Authorization": 'Bearer ${twitter.token}', //twitter.token is the token recieved from Twitter sign in process
"Content-Type": "application/json"
});
returns "errors":[{"code":89,"message":"Invalid or expired token."}]
我知道令牌有效
编辑 2:
Future<List<Tweet>> getTimeline() async {
print("Getting timeline");
var query = https.get(
"https://api.twitter.com/1.1/statuses/home_timeline.json?count=2&tweet_mode=extended&exclude_replies=false",
headers: {
"Authorization":
'oauth_consumer_key="$_consumerKey", oauth_token="${twitter.token}"',
"Content-Type": "application/json"
}).timeout(Duration(seconds: 15));
print("Before await");
final response = await query;
print("code: ${response.statusCode}");
...
}
经过更多调试后,twitter.token 可能出现空异常。修复后,我仍然收到错误的授权数据。我会继续尝试向 header 添加更多信息,看看是否有帮助。
编辑 3:
这是我生成签名的方法:
static String generateSignature(String method, String base, List<String> sortedItems) {
String sig = '$method&${Uri.encodeComponent(base)}&';
String param = '';
for (int i = 0; i < sortedItems.length; i++) {
if (i == 0)
param = sortedItems[i];
else
param += '&${sortedItems[i]}';
}
sig += Uri.encodeComponent(param);
String key = '${Uri.encodeComponent(_secretKey)}&${Uri.encodeComponent(twitter.secret)}';
var digest = Hmac(sha1, utf8.encode(key)).convert(utf8.encode(sig));
print("base: ${digest.bytes}");
print("sig: ${base64.encode(digest.bytes)}");
return base64.encode(digest.bytes);
}
时间线方法如下:
Future<List<Tweet>> getTimeline() async {
print("Getting timeline");
Future<http.Response> query;
try {
String base = 'https://api.twitter.com/1.1/statuses/home_timeline.json';
String count = 'count=2';
String mode = 'tweet_mode=extended';
String replies = 'exclude_replies=false';
String oauthConsumer = 'oauth_consumer_key="$_consumerKey"';
String oauthToken = 'oauth_token="${twitter.token}"';
String oauthNonce = 'oauth_nonce="${randomAlphaNumeric(20)}"';
String oauthVersion = 'oauth_version="1.0"';
String oauthTime =
'oauth_timestamp="${DateTime.now().millisecondsSinceEpoch}"';
String oauthMethod = 'oauth_signature_method="HMAC-SHA1"';
String oauthSig = 'oauth_signature="${generateSignature("GET", base, [
count,
replies,
oauthConsumer,
oauthNonce,
oauthTime,
oauthToken,
oauthVersion,
mode
])}"';
query = http.get(
new Uri.https("api.twitter.com", "/1.1/statuses/home_timeline.json", {
"count": "2",
"tweet_mode": "extended",
"exclude_replies": "false"
}),
headers: {
"Authorization": '$oauthConsumer, $oauthToken, $oauthVersion, $oauthTime, $oauthNonce, $oauthMethod, $oauthSig',
"Content-Type": "application/json"
}).timeout(Duration(seconds: 15));
} catch (e) {
print(e);
}
谢谢!
这是 Twitter 用户身份验证的文档:https://developer.twitter.com/en/docs/basics/authentication/overview/3-legged-oauth
前 3 个步骤由 flutter_twitter_login 处理。看看最后一个例子:
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
另一个例子可以在这里找到: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'
我不确定您是否需要所有这些参数,但是 header 对于一个简单的 get 请求可能至少需要这个:
headers: {
"Authorization": 'oauth_consumer_key="${consumerKey}", oauth_token="${twitter.token}"',
"Content-Type": "application/json"
}
这是最终运行的代码:
生成字符串方法:
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);
}
twitter get 调用的便捷方法:
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));
}
调用示例:
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;
}
}