I am trying to utilise the twitter API with rust but I can't seem to access the struct allows for Oauth2token validation.
I have tried using the bearer token with both the crate twitter_v2 and making requests to the end points directly.
Here is my code.
Twitter.rs
pub mod Twitter {
use twitter_v2::TwitterApi;
use twitter_v2::authorization::{Oauth2Token,BearerToken};
use serde_json;
pub fn authorise() -> TwitterApi<BearerToken> {
println!("{}", std::env::var("BEARER_TOKEN").unwrap());
let auth = BearerToken::new(std::env::var("BEARER_TOKEN").unwrap());
println!("Successfully authorised.");
TwitterApi::new(auth)
}
pub fn auth2authorise() {
let auth: Oauth2Token = todo!();
let my_followers = TwitterApi::new(auth);
}
}
main.rs
pub mod twitter;
use twitter::Twitter::authorise;
use dotenv::dotenv;
use twitter_v2::query::TweetField;
#[tokio::main]
async fn main() {
dotenv().ok();
let tweeter = authorise();
let data = tweeter.get_tweet(1261326399320715264)
.tweet_fields([TweetField::AuthorId, TweetField::CreatedAt])
.send()
.await.unwrap()
.into_data()
.expect("this tweet should exist");
println!("{}",data.text);
}
And here is the error message;
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value:
Api(ApiError {
title: "Client Forbidden",
kind: "https://api.twitter.com/2/problems/client-forbidden",
status: 403,
detail: "When authenticating requests to the Twitter API v2 endpoints, you must use keys and tokens from a Twitter developer App that is attached to a Project. You can create a project via the developer portal.",
errors: []
})', src/main.rs:13:12
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace