今後数か月で廃止されるクライアントシークレットとID [重複]

Aug 20 2020

私のコードは機能しますが、リクエスト形式は今後数か月で非推奨になります。

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,
    };
  }
}

回答

2 Phil Aug 20 2020 at 06:54

-uオプションcurlはHTTP基本認証を使用します。

これが行うことは、user:password文字列を取得し、base64でエンコードし(例user:password:> dXNlcjpwYXNzd29yZA==)、次のようにAuthorizationリクエストヘッダーに追加します

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