Last active
February 19, 2021 12:40
-
-
Save aiya000/377d12132ca2bd9a525715241531b5fa to your computer and use it in GitHub Desktop.
Getting an access token of VRChat API. and then getting friend list using it. VRChat APIでアクセストークンを使って、フレンドを取得するやつ。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios from 'axios' | |
function isThatArray<T>(x: unknown, p: (a: unknown) => a is T): x is Array<T> { | |
return Array.isArray(x) && x.every(p) | |
} | |
function isString(x: unknown): x is string { | |
return typeof x === 'string' | |
} | |
const vrchatApi = { | |
url: 'https://api.vrchat.cloud/api/1' as const, | |
apiKey: 'JlE5Jldo5Jibnk5O5hTx6XVqsJu4WJ26' as const, | |
} as const | |
async function main(): Promise<void> { | |
try { | |
const auth = process.argv[2] | |
console.log('auth:', auth) | |
if (auth === undefined) { | |
throw new Error('Give me an authorization.') | |
} | |
const response = await axios.get(`${vrchatApi.url}/auth/user?apiKey=${vrchatApi.apiKey}`, { | |
headers: { | |
Origin: 'https://vrchat.com', | |
Authorization: `Basic ${auth}`, | |
}, | |
}) | |
const cookie: unknown = response.headers['set-cookie'] | |
if (!isThatArray(cookie, isString)) { | |
throw new Error(`response.headers["set-cookie"] is it not an array: ${cookie}`) | |
} | |
const tokenCookie = cookie.find((x) => /^auth=/.test(x)) | |
if (tokenCookie === undefined) { | |
throw new Error('The accessToken of the specified user is not found.') | |
} | |
const token = tokenCookie.split(/^auth=([^;]+);/)[1] | |
if (token === undefined) { | |
throw new Error('Exctracting the access token is failed') | |
} | |
console.log(token) | |
} catch (e) { | |
console.log('Fatal error:', e) | |
} | |
} | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios from 'axios' | |
const vrchatApi = { | |
url: 'https://api.vrchat.cloud/api/1' as const, | |
apiKey: 'JlE5Jldo5Jibnk5O5hTx6XVqsJu4WJ26' as const, | |
} as const | |
async function main(): Promise<void> { | |
try { | |
const token = process.argv[2] | |
if (token === undefined) { | |
throw new Error('Give me an access token.') | |
} | |
const response = await axios.get( | |
`${vrchatApi.url}/auth/user/friends?apiKey=${vrchatApi.apiKey}`, | |
{ | |
headers: { | |
Origin: 'https://vrchat.com', | |
Cookie: `auth=${token}`, | |
}, | |
}, | |
) | |
console.log(response) | |
} catch (e) { | |
console.error('Fatal error:', e) | |
} | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
VRChat APIでアクセストークンを使って、フレンドを取得するやつ。