What's causing the 'API rate limit exceeded' error in Rust's Reqwest even though I've included my Github token?

huangapple go评论60阅读模式
英文:

What's causing the 'API rate limit exceeded' error in Rust's Reqwest even though I've included my Github token?

问题

尽管代码中包含了GitHub令牌,但由于某种原因,我仍然无法使用更多的GitHub Rest API配额。

我正在使用Rust中的Reqwest来处理所有请求。我认为可能是我输入令牌时出错了。

代码

        let mut headers = HeaderMap::new();
        headers.insert(USER_AGENT, HeaderValue::from_static("SBSixteen"));
        headers.insert(AUTHORIZATION,HeaderValue::from_static("github_pat_****************************************************") );
        let client = Client::builder().default_headers(headers).build().unwrap();
        let url = format!("https://api.github.com/users/{}",data.git_username);

        println!("{}", &url);

        let r1 = client.get(&url).send().await.unwrap().text().await.unwrap();
        println!("{}", &r1);

错误

{"message":"API rate limit exceeded for ***.***.**.*. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)","documentation_url":"https://docs.github.com/rest/overview/resources-in-the-rest-api#rate-limiting"}
thread 'tokio-runtime-worker' panicked at 'called `Option::unwrap()` on a `None` value', src\NER_EZH.rs:632:38
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

我尝试了多种可能的组合,但仍然无法正确设置我的令牌。

英文:

Although the code has the Github token included, for some reason I am still unable to avail more GitHub Rest API quota.

I am using Reqwest for Rust to process all requests. I believe that I may be entering my token incorrectly.

The Code

        let mut headers = HeaderMap::new();
        headers.insert(USER_AGENT, HeaderValue::from_static("SBSixteen"));
        headers.insert(AUTHORIZATION,HeaderValue::from_static("github_pat_****************************************************") );
        let client = Client::builder().default_headers(headers).build().unwrap();
        let url = format!("https://api.github.com/users/{}",data.git_username);

        println!("{}", &url);

        let r1 = client.get(&url).send().await.unwrap().text().await.unwrap();
        println!("{}", &r1);

The Error

{"message":"API rate limit exceeded for ***.***.**.*. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)","documentation_url":"https://docs.github.com/rest/overview/resources-in-the-rest-api#rate-limiting"}
thread 'tokio-runtime-worker' panicked at 'called `Option::unwrap()` on a `None` value', src\NER_EZH.rs:632:38
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

I tried multiple possible combinations but I am unable to set my token correctly.

答案1

得分: 2

GitHub API文档中,我们可以看到授权令牌应该以"bearer tokens"的形式呈现。只需在令牌前加上 "Bearer " 前缀:

headers.insert(
    AUTHORIZATION,
    HeaderValue::from_static("Bearer github_pat_***..."),
);
英文:

From the GitHub API documentation, we can see that authorization tokens are expected to be presented as "bearer tokens." You just need to prefix the token with "Bearer ":

headers.insert(
    AUTHORIZATION,
    HeaderValue::from_static("Bearer github_pat_***..."),
);

huangapple
  • 本文由 发表于 2023年5月26日 10:11:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76337250.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定