본문 바로가기
IT/Nodejs

imgur access key 생성 방법

by DOSGamer 2022. 8. 25.
반응형

imgur.com 에 이미지를 프로그램으로 올리려고 하는데 방법을 몰라서 한참 헤매다가 정리해놓습니다

imgur api 를 참고했습니다

Imgur API
image](https://i.imgur.com/n744BL9.png) ## API Status Status for the API can be found at [status.imgur.com](http://status.imgur.com)! ## Getting Started Imgur's API exposes the entire Imgur infrastructure via a standardized programmatic interface. Using Imgur's API, you can do just about anything you can do on imgur.com, while using your programming language of choice.
https://apidocs.imgur.com/

imgur 라이브러리도 참고했습니다

https://github.com/KenEucker/imgur

Imgur 사이트에서 할일

  1. imgur.com 에 회원가입
  1. imgur.com 에 application 등록
    1. 이름은 임의대로
    1. callback URL 은 반드시 : https://www.getpostman.com/oauth2/callback
  1. application 등록시 나오는 client_id, client_secret 메모

postman 에서 할일

  1. postman 설치
  1. postman 에서 http 한개 생성
  1. Authorization 탭에 OAuth2.0 선택
  1. Configure New Token
    1. Token Name : 임의대로 작성
    1. CallBack URL : https://www.getpostman.com/oauth2/callback imgur.com 에서 등록한 callback 과 동일
    1. Auth URL : https://api.imgur.com/oauth2/authorize
    1. Access Token URL : https://api.imgur.com/oauth2/token
    1. Client ID : imgur 에서 app 등록시 발행된 Client ID
    1. Client Secret : imgur 에서 app 등록시 발행된 Client Secret
    1. Get New Access Token 버튼 클릭

그럼 포스트맨에서 Access Token 과 Refresh Token 을 받아서 화면에 뿌려줍니다

imgur 라이브러리 활용한 소스코드

access token 은 환경변수로 뺐습니다.

아래 코드는 puppeteer 로 이미지 만들어서 imgur 에 이미지를 올리려고 했던 겁니다


// imgurUpload.js
const fs = require('fs');
const { ImgurClient } = require('imgur');

export const uploadChartImage = async (page, imageName) => {
  const client = new ImgurClient({
    accessToken: process.env.IMGUR_ACCESS_TOKEN,
  });
  const readStream = fs.createReadStream(imageName);

  const response = await client.upload({
    image: readStream,
    type: 'stream',
    album: process.env.IMGUR_ALBUM_ID,
  });
  console.log(response.data);
  return response.data.link;
};

Imgur 앨범 id 확인 방법

  1. imgur 사이트에 로그인
  1. images 메뉴 선택
  1. 앨범 선택 하면 Edit this album 옵션 창이 보입니다
  1. Embed Album 선택
  1. 그러면 스크립트가 나옵니다. 거기에서 data-id 의 값 중에 a/ 를 제외하면 앨범ID 입니다

Uploaded by N2T

반응형