所以在twitter api 文档中,它说要生成一个承载令牌,你运行这个命令
curl --user "$API_KEY:$API_SECRET_KEY" \
--data 'grant_type=client_credentials' \
'https://api.twitter.com/oauth2/token'
我运行它,我得到的结果是https://api.twitter.com/ouath2/token的 HTML
然后我写了一些生锈的代码,
extern crate reqwest;
use reqwest::{ Client };
// Twitter api endpoint - https://api.twitter.com/
#[tokio::main]
async fn main() -> Result<(), reqwest::Error>{
let http_client =Client::new();
// We need to get a bearer_token before we can access anything with the twitter api
let bearer_token = http_client.post("https://api.twitter.com/oauth2/token")
.basic_auth(API_KEY, Some(API_KEY_SECRET)
.body("grant_type=client_credentials")
.send().await?
.text().await?;
println!("{}", bearer_token);
Ok(())
}
执行上面的 rust 代码后,打印出来的是这样的,
{"errors":[{"code":170,"message":"Missing required parameter: grant_type","label":"forbidden_missing_parameter"}]}
但是,从文档来看,预期的结果是:
{"token_type":"bearer","access_token":"AAAAAAAAAAAAAAAAAAAAAMLheAAAAAAA0%2BuSeid%2BULvsea4JtiGRiSDSJSI%3DEUifiRBkKG5E2XzMDjRfl76ZC9Ub0wnz4XsNiRVBChTYbJcE3F"}
有谁知道为什么?这是我的 cargo.toml 文件
[package]
name = "release"
version = "0.1.0"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
reqwest = { version = "0.11", features = ["json"] }
tokio = { version = "1", features = ["full"] }
顺便说一句,我在几个小时前申请了 twitter 开发人员访问权限,但我不确定如何检查我是否有访问权限。我的意思是,我可以创建一个项目、一个应用程序并访问仪表板。