Client secret & id 将在未来几个月内弃用

Client secret & id to be deprecated in the coming months

我的代码有效,但请求格式将在未来几个月内弃用。

Github

建议的新格式
curl -u my_client_id:my_client_secret https://api.github.com/users/user

谁能告诉我重新格式化它的正确方法,以便在未来几个月内弃用它。我已经尝试了一切。这是我尝试过的一个例子:

我的尝试无效

`-u ${this.client_id}:${this.client_secret} https://api.github.com/users/${user}`

我现在的代码

class Github {
  constructor() {
    // THESE ARE FAKE!!!
    this.client_id = 'a71344259aec03d0cea3';
    this.client_secret = 'a28202377336e199cb554bd099e6e5fe672788db';
    this.repos_count = 7;
    this.repos_sort = 'created: asc';
  }

  async getUser(user) {
    const profileResponse = await fetch(
      `https://api.github.com/users/${user}?client_id=${this.client_id}&client_secret=${this.client_secret}`
    );

    const repoResponse = await fetch(
      `https://api.github.com/users/${user}/repos?per_page=${this.repos_count}&sort=${this.repos_sort}&client_id=${this.client_id}&client_secret=${this.client_secret}`
    );
    console.log(user);

    const profile = await profileResponse.json();
    const repos = await repoResponse.json();

    return {
      profile,
      repos,
    };
  }
}

curl中的-u选项使用HTTP Basic authentication

它所做的是获取 user:password 字符串,对其进行 base64 编码(例如 user:password => dXNlcjpwYXNzd29yZA==)并将其添加到 Authorization 请求 header像这样

Authorization: Basic dXNlcjpwYXNzd29yZA==

使用 fetch 时,您必须使用 btoa()

之类的方法手动执行此操作
const auth = btoa(`${this.client_id}:${this.client_secret}`)

fetch(`https://api.github.com/users/${encodeURIComponent(user)}`, {
  headers: {
    Authorization: `Basic ${auth}`
  }
})