1+ const uuid = require ( 'uuid' ) . v4
12const fs = require ( 'fs' )
23const fetch = require ( 'isomorphic-fetch' )
34const querystring = require ( 'querystring' )
45const FormData = require ( 'form-data' )
56const { session} = require ( 'electron' )
67const stream = require ( 'stream' )
7- const Oauth2infoReader = require ( '../oauth/OauthInfoReader' )
8+ const OauthInfoReader = require ( '../oauth/OauthInfoReader' )
89const appInfo = require ( '../appInfo' )
910const ExternalOAuth2 = require ( '../oauth/ExternalOAuth2' ) ;
11+ const OAuthRequestManager = require ( '../oauth/OAuthRequestManager' ) ;
1012
1113const errorHandler = ( res ) => {
1214 if ( ! res . ok ) {
@@ -19,18 +21,30 @@ const errorHandler = (res) => {
1921}
2022
2123const BASE_URL = 'https://www.tistory.com/apis'
24+ const PROVIDER_ID = 'tistory'
2225
23- const requestAuth = ( ) => {
24- const oauth2infoReader = new Oauth2infoReader ( )
25- const tistoryOAuth = new ExternalOAuth2 ( oauth2infoReader . getTistory ( ) )
26- tistoryOAuth . requestAuth ( { } )
27- return tistoryOAuth . getState ( )
28- }
26+ const requestAuth = ( successHandler , failureHandler ) => {
27+ const oauthInfoReader = new OauthInfoReader ( )
28+ const oauth2 = new ExternalOAuth2 ( oauthInfoReader . getTistory ( ) )
29+ OAuthRequestManager . saveRequestInfo ( "oauth" , ( searchParams ) => {
30+ const code = searchParams . get ( "code" )
31+ oauth2 . requestToken ( code , 'GET' )
32+ . then ( data => {
33+ if ( data . error ) {
34+ throw new Error ( `${ data . error } : ${ data . error_description } ` )
35+ }
36+
37+ return {
38+ uuid : uuid ( ) ,
39+ provider : PROVIDER_ID ,
40+ authInfo : data
41+ }
42+ } )
43+ . then ( successHandler )
44+ . catch ( failureHandler )
45+ } )
2946
30- const requestToken = ( code ) => {
31- const oauth2infoReader = new Oauth2infoReader ( )
32- const tistoryOAuth = new ExternalOAuth2 ( oauth2infoReader . getTistory ( ) )
33- return tistoryOAuth . requestToken ( code , 'GET' )
47+ oauth2 . requestAuth ( { } )
3448}
3549
3650const fetchBlogInfo = ( auth ) => {
@@ -252,7 +266,6 @@ const fetchAccount = async (auth) => {
252266
253267module . exports = {
254268 requestAuth : requestAuth ,
255- requestToken : requestToken ,
256269 fetchBlogInfo : fetchBlogInfo ,
257270 fetchUser : fetchUser ,
258271 fetchPosts : fetchPosts ,
0 commit comments