[개발][자동화][NODEJS] TISTORY API로 글쓰기 - 1 by wonsama

View this thread on steempeak.com
· @wonsama ·
$12.20
[개발][자동화][NODEJS] TISTORY API로 글쓰기 - 1
# 1. 개요

> 참조 문서 : https://tistory.github.io/document-tistory-apis/
>
> 최종 작업일 2020.02.18 ( 참조용 )

* 목표 1 : 스팀잇에 쓰인 글을 주소 입력만으로 TISTORY 로 바로 등록하기
* 목표 2 : 자동으로 TISTORY 에 주기적으로 글 작성하기

# 2. 작업환경

* nodejs + express : 최신버전으로 설치하면 무난

# 3. TISTORY API 키 발급받기

#### 3.1. 등록페이지 접속 : https://www.tistory.com/guide/api/manage/register

![](https://cdn.steemitimages.com/DQmXb15VGojciXRRMGMVRqCCxjFFwRs3L893dcm97r8X3C2/image.png)

> 이메일주소와 ID를 입력하여 로그인 

#### 3.2. 앱 등록 - 정보 등록

> CallBack 이 중요. 나머지는 적당히 입력해도 상관 없음.

![](https://cdn.steemitimages.com/DQmU89VxSTKcfmdmXcA9DLR2pTS1KUwcCwXGg7Kt3AmmrHj/image.png)

#### 3.3. 앱 관리 - 정보확인

> APP ID 와 Secret Key 는 매우 중요한 정보이므로 타인에게 노출되지 않도록 조심하기 바랍니다.

![](https://cdn.steemitimages.com/DQmcmMKC1gYDTPmBdmADMzdrMCjRpgw8qL7J4RYdhXyfFi1/image.png)


# 4. NODEJS + EXPRESS

> 웹서비스를 구축해서 ACCESS_TOKEN 정보를 받아보도록 하겠습니다.

#### 4.1. 설치 정보

* nodejs 설치 : https://nodejs.org 접속 후 download 및 설치(다음 or next 만 잘 누름 됨 )
* express 설치 

> 웹 프레임워크는 expressjs, 웹 호출용 request (또는 axios 설치해도 됨 ) , node 모니터링용 nodemon 이렇게 3개를 설치 

```
$ mkdir web
$ cd web
$ npm init
(이후 enter 만 쳐서 일단 기본값으로 완료)
$ npm install express request
$ npm install -g nodemon
```

#### 4.2. 파일 수정 및 생성

* package.json 수정

> "main" : "app.js" 로 수정

* app.js 파일 생성

> 인증에 필요한 부분만 구현

```js
const express = require('express');
const path = require('path');
const request = require('request');

// init
const app = express();

// 라우팅 설정
app.get('/tistory/auth/res', function(req,res,body){
	// client_id, redirect_uri 값 입력을 통해 Authentication Code 값을 얻어 온다.
	// https://www.tistory.com/oauth/authorize?client_id=1fd6a4c18027723413ebedbb0b58c1f2&redirect_uri=http://localhost:3000/tistory/auth/res&response_type=code

  let client_id = '위에서 생성한 자신의 APP ID 입력';
  let client_secret = '위에서 생성한 자신의 Secret Key 입력';
  let code = req.query.code;
  let redirect_uri = 'http://localhost:3000/tistory/auth/res';	// 사용은 안되는데 누락되면 오류
  let url = `https://www.tistory.com/oauth/access_token?client_id=${client_id}&client_secret=${client_secret}&code=${code}&redirect_uri=${redirect_uri}&grant_type=authorization_code`;
	let options = {
			url,
			method : 'get',
			timeout : 1000,
		};

	request(options, function (error, response, body) {
		if(error){
			res.send(error);
		}else{
			res.send(body);
		}
	});
});

// start server
const SERVER_PORT = 3000;
app.listen(SERVER_PORT, function(){
	console.log(`server is started with http://localhost:${SERVER_PORT}`);
})

```

#### 4.3. 웹서버 실행

![](https://cdn.steemitimages.com/DQmY6Hvb9BoLFhnExzCozNWavRpkgejt5eaBGMTsrPtxFaz/image.png)

> $ nodemon

# 5. ACCESS_TOKEN 확인하기

#### 5.1. 인증 페이지 접속

![](https://cdn.steemitimages.com/DQmQaoNoj5sWhZvrm6rpU7AKm7ivwrAja1y2MtddT5cR19X/image.png)

https://www.tistory.com/oauth/authorize?client_id=MY_CLIENT_ID&redirect_uri=http://localhost:3000/tistory/auth/res&response_type=code

> 위 주소 값에서 `client_id`, `redirect_uri` 위 2가지 값을 확인한 후 접속한 다음 확인 버튼을 눌러 ACCESS_TOKEN 값을 확인한다

_결과 예시_

```
access_token=a39f1d**********************************
```

# 맺음말

일단 ACCESS_TOKEN 을 확보 했다면 뭐 거의 다 완료 했다고 보면 됩니다. 이제 이것을 이용하여 글을 작성하면 됩니다. (다음편에 계속 ... ) https://tistory.github.io/document-tistory-apis/ 링크를 참조하면 API 활용 방법을 볼 수 있습니다.
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 38 others
properties (23)
post_id84,493,753
authorwonsama
permlinknodejs-tistory-api-1
categoryzzan
json_metadata{"tags":["zzan","sct","sct-freeboard","sct-kr","steemleo","palnet","liv","mini","busy","dblog"],"image":["https:\/\/cdn.steemitimages.com\/DQmXb15VGojciXRRMGMVRqCCxjFFwRs3L893dcm97r8X3C2\/image.png","https:\/\/cdn.steemitimages.com\/DQmU89VxSTKcfmdmXcA9DLR2pTS1KUwcCwXGg7Kt3AmmrHj\/image.png","https:\/\/cdn.steemitimages.com\/DQmcmMKC1gYDTPmBdmADMzdrMCjRpgw8qL7J4RYdhXyfFi1\/image.png","https:\/\/cdn.steemitimages.com\/DQmY6Hvb9BoLFhnExzCozNWavRpkgejt5eaBGMTsrPtxFaz\/image.png","https:\/\/cdn.steemitimages.com\/DQmQaoNoj5sWhZvrm6rpU7AKm7ivwrAja1y2MtddT5cR19X\/image.png"],"links":["https:\/\/tistory.github.io\/document-tistory-apis\/","https:\/\/www.tistory.com\/guide\/api\/manage\/register","https:\/\/nodejs.org","https:\/\/www.tistory.com\/oauth\/authorize?client_id=MY_CLIENT_ID&redirect_uri=http:\/\/localhost:3000\/tistory\/auth\/res&response_type=code"],"app":"steemcoinpan\/0.1","format":"markdown","canonical_url":"https:\/\/www.steemcoinpan.com\/@wonsama\/nodejs-tistory-api-1"}
created2020-02-18 02:49:54
last_update2020-02-18 02:49:54
depth0
children4
net_rshares39,350,151,185,649
last_payout2020-02-25 02:49:54
cashout_time1969-12-31 23:59:59
total_payout_value6.332 SBD
curator_payout_value5.864 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length3,184
author_reputation959,891,668,598,481
root_title"[개발][자동화][NODEJS] TISTORY API로 글쓰기 - 1"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (102)
@slowdive14 ·
전 제가 쓴 티스토리 블로그 글을 스팀잇에 퍼오는 쪽인데 원사마님께서 올리신 이 방법은 스팀잇에 글 쓰면 자동으로 티스토리에 등록되게 하는 것인가요? 유용해 보입니다
properties (22)
post_id84,495,014
authorslowdive14
permlinkq5vp5q
categoryzzan
json_metadata{"app":"steemit\/0.1"}
created2020-02-18 03:46:39
last_update2020-02-18 03:46:39
depth1
children1
net_rshares0
last_payout2020-02-25 03:46:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length92
author_reputation17,601,738,132,884
root_title"[개발][자동화][NODEJS] TISTORY API로 글쓰기 - 1"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@wonsama ·
넵 정방향 역방향 둘 다 가능합죠 .

최종 목표는 온갖 블로그를 한번에 포스팅 하는 것인지라 ㅋ
properties (22)
post_id84,499,164
authorwonsama
permlinkq5vzgz
categoryzzan
json_metadata{"app":"steemit\/0.1"}
created2020-02-18 07:29:24
last_update2020-02-18 07:29:24
depth2
children0
net_rshares0
last_payout2020-02-25 07:29:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length54
author_reputation959,891,668,598,481
root_title"[개발][자동화][NODEJS] TISTORY API로 글쓰기 - 1"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@zzings ·
저도 쓸수 있게 해주세요
properties (22)
post_id84,496,738
authorzzings
permlinkq5vtt2
categoryzzan
json_metadata{"app":"steemit\/0.1"}
created2020-02-18 05:27:03
last_update2020-02-18 05:27:03
depth1
children1
net_rshares0
last_payout2020-02-25 05:27:03
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length13
author_reputation1,646,897,865,482,869
root_title"[개발][자동화][NODEJS] TISTORY API로 글쓰기 - 1"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@wonsama ·
다 완성 돠면 촤대한 쉽게 해보죵 ㅎㅎ
properties (22)
post_id84,499,172
authorwonsama
permlinkq5vzhp
categoryzzan
json_metadata{"app":"steemit\/0.1"}
created2020-02-18 07:29:51
last_update2020-02-18 07:29:51
depth2
children0
net_rshares0
last_payout2020-02-25 07:29:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length21
author_reputation959,891,668,598,481
root_title"[개발][자동화][NODEJS] TISTORY API로 글쓰기 - 1"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000