skip to Main Content

So over at the twitter api documentation, it says to generate a Bearer token you run this command

curl --user "$API_KEY:$API_SECRET_KEY" 
  --data 'grant_type=client_credentials' 
  'https://api.twitter.com/oauth2/token'

I run it and the result I get is the HTML for https://api.twitter.com/ouath2/token

Then I wrote some rust code,

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(())
}

After executing the rust code above, this is what is printed,

{"errors":[{"code":170,"message":"Missing required parameter: grant_type","label":"forbidden_missing_parameter"}]}

However from the documentation the expected result is this:

{"token_type":"bearer","access_token":"AAAAAAAAAAAAAAAAAAAAAMLheAAAAAAA0%2BuSeid%2BULvsea4JtiGRiSDSJSI%3DEUifiRBkKG5E2XzMDjRfl76ZC9Ub0wnz4XsNiRVBChTYbJcE3F"}

Does anyone know why?
Here’s my cargo.toml file

[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"] }

By the way, I applied for twitter developer access a few hours back, but I’m not sure on how to check if I do have access. I mean, I can make a project, and an app and access the dashboard.

2

Answers


  1. https://api.twitter.com/2/oauth2/token is the wrong URI, it should be https://api.twitter.com/oauth2/token

    Login or Signup to reply.
  2. Are there any brackets missing?

    .basic_auth(API_KEY, Some(API_KEY_SECRET)
    
    .basic_auth(API_KEY, Some(API_KEY_SECRET))
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search