我过去曾多次使用过Twitter API,熟悉身份验证流程。我正在使用我自己创建的基于Dart的客户端实现一个Twitter机器人(link),一切运行良好,从GET到POST再到流API请求。
但是,我正在努力发布包含URL的推文。使用/statuses/update.json
端点,我可以使用纯文本成功发布推文,例如:
@thosakwe Hello, world!
但是,一旦我添加了status
参数的链接,我就会收到Could not authenticate you.
错误。我怀疑我可能需要对我的状态中的链接进行URL编码,但我仍然会遇到同样的错误。
有谁知道这里有什么问题吗?提前谢谢。
编辑:这是我的代码。您可能不熟悉Dart
,但这实际上只是NPM twit
库的一个端口。
const String ANNOUNCE_BETA =
'https://medium.com/the-angel-framework/announcing-angel-v1-0-0-beta-46dfb4aa8afe';
const List<String> STATUSES = const [
'@<handle> A #dartlang fan like you should take a look at the Angel server framework! $ANNOUNCE_BETA',
'@<handle> Angel, a #dartlang server framework, is now in beta. Take it for a spin? $ANNOUNCE_BETA',
'@<handle> Angel is a rising full-stack server framework for #dartlang. Check it out! $ANNOUNCE_BETA',
'@<handle> You can take your #dartlang skills onto the server, too! Check out Angel! $ANNOUNCE_BETA',
'@<handle> Have you tried Angel, a full-stack #dartlang server framework? Now in beta! $ANNOUNCE_BETA'
];
final RegExp _link = new RegExp(
r'https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,4}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)');
String chooseStatus(Random rnd, String handle) {
return STATUSES[rnd.nextInt(STATUSES.length)]
.replaceAll('<handle>', handle)
.replaceAllMapped(_link, (m) => Uri.encodeComponent(m[0]));
}
Future handleTimeline(Twit twit, Map tweet, String myHandle, Random rnd) async {
Map user = tweet['user'];
String tweetId = tweet['id_str'],
text = tweet['text'],
handle = user['screen_name'];
if (handle != myHandle) {
print("Responding to tweet from @$handle: '$text'");
var status = chooseStatus(rnd, handle);
await twit.post('/statuses/update.json',
{'status': status, 'in_reply_to_status_id': tweetId});
}
}