1

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
Dai
  • 141,631
  • 28
  • 261
  • 374
  • There's nothing in your `Twitter::authorise()` method that does anything useful - it just loads a token from your `BEARER_TOKEN` environment-variable - so where/how are you getting the `BEARER_TOKEN` value in the first place? – Dai Jun 24 '23 at 08:09
  • I generated it from the Twitter API, and that one actually loads, though somehow I am still getting access restrictions. Apparently, the only lead I got showed that I should be using Oauth2token authentication to access those endpoints. – Ssali Benjamin Jun 24 '23 at 08:27
  • Do you know how to use it by any chance? – Ssali Benjamin Jun 24 '23 at 08:31
  • Even if I did, I wouldn't want to be seen supporting Elon Musk's right-wing media machine. – Dai Jun 24 '23 at 08:38
  • Haha... alright. Thanks for the edit though. – Ssali Benjamin Jun 24 '23 at 08:47

0 Answers0