diff --git a/lib/routes/pixiv/token.ts b/lib/routes/pixiv/token.ts index ac0d9e542..98356a057 100644 --- a/lib/routes/pixiv/token.ts +++ b/lib/routes/pixiv/token.ts @@ -15,34 +15,27 @@ const refreshToken = (tryGet) => tryGet( 'pixiv:accessToken', () => - got - .post('https://oauth.secure.pixiv.net/auth/token', { - form: { - ...authorizationInfo, - get_secure_url: 1, - grant_type: 'refresh_token', - refresh_token: config.pixiv.refreshToken, - }, - headers: { - ...maskHeader, - }, - responseType: 'json', - resolveBodyOnly: true, - }) - .catch((error) => { - logger.error('Pixiv refresh token failed.'); - logger.error(error); - }), + got.post('https://oauth.secure.pixiv.net/auth/token', { + form: { + ...authorizationInfo, + get_secure_url: 1, + grant_type: 'refresh_token', + refresh_token: config.pixiv.refreshToken, + }, + headers: { + ...maskHeader, + }, + }), 3600, false ); async function getToken(tryGet) { - const result = await refreshToken(tryGet); + const { data } = await refreshToken(tryGet); // let expireTime; - if (result && result.access_token) { + if (data && data.access_token) { logger.debug('Pixiv refresh token success.'); - token = result.access_token; + token = data.access_token; // expireTime = result.expires_in; } // } else { diff --git a/lib/utils/got.ts b/lib/utils/got.ts index 6701fd502..a602fab3a 100644 --- a/lib/utils/got.ts +++ b/lib/utils/got.ts @@ -9,12 +9,7 @@ const gotofetch = ofetch.create({ }); const getFakeGot = (defaultOptions?: any) => { - const fakeGot = (request, options) => { - if (options?.hooks?.beforeRequest) { - for (const hook of options.hooks.beforeRequest) { - hook(options); - } - } + const fakeGot = (request, options?: any) => { if (!(typeof request === 'string' || request instanceof Request) && request.url) { options = { ...request, @@ -22,6 +17,12 @@ const getFakeGot = (defaultOptions?: any) => { }; request = request.url; } + if (options?.hooks?.beforeRequest) { + for (const hook of options.hooks.beforeRequest) { + hook(options); + } + delete options.hooks; + } options = { ...defaultOptions, @@ -30,6 +31,7 @@ const getFakeGot = (defaultOptions?: any) => { if (options?.json && !options.body) { options.body = options.json; + delete options.json; } if (options?.form && !options.body) { const body = new FormData(); @@ -40,7 +42,11 @@ const getFakeGot = (defaultOptions?: any) => { if (!options.headers) { options.headers = {}; } - options.headers['Content-Type'] = 'application/x-www-form-urlencoded'; + delete options.form; + } + if (options?.searchParams) { + request += '?' + new URLSearchParams(options.searchParams).toString(); + delete options.searchParams; } return gotofetch(request, options);