클라이언트 비밀번호 및 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}`
  }
})