For Twitter API v2.
For usage, see OAuth 2.0 provider with PKCE.
import { Twitter } from "arctic";
const twitter = new Twitter(clientId, clientSecret, redirectURI);
const url: URL = await twitter.createAuthorizationURL(state, codeVerifier, {
// optional
scopes
});
const tokens: TwitterTokens = await twitter.validateAuthorizationCode(code, codeVerifier);
const tokens: TwitterTokens = await twitter.refreshAccessToken(refreshToken);
Get user profile
Add the users.read
and tweet.read
scopes and use the /users/me
endpoint. You cannot get user emails with the v2 API.
const url = await twitter.createAuthorizationURL(state, codeVerifier, {
scopes: ["users.read", "tweet.read"]
});
const tokens = await twitter.validateAuthorizationCode(code, codeVerifier);
const response = await fetch("https://api.twitter.com/2/users/me", {
headers: {
Authorization: `Bearer ${tokens.accessToken}`
}
});
const user = await response.json();
Get refresh token
Add the offline.access
scope to get refresh tokens.
const url = await twitter.createAuthorizationURL(state, codeVerifier, {
scopes: ["users.read", "tweet.read", "offline.access"]
});