Skip to content

Instantly share code, notes, and snippets.

@aiya000
Last active February 19, 2021 12:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aiya000/377d12132ca2bd9a525715241531b5fa to your computer and use it in GitHub Desktop.
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でアクセストークンを使って、フレンドを取得するやつ。
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()
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()
@aiya000
Copy link
Author

aiya000 commented Feb 17, 2021

VRChat APIでアクセストークンを使って、フレンドを取得するやつ。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment