ベアラートークン(App のみの認可)
quickstart.ts
Copy
Ask AI
import {
Client,
type ClientConfig,
type Users
} from '@xdevplatform/xdk';
const config: ClientConfig = { bearerToken: 'your-bearer-token' };
const client: Client = new Client(config);
async function main(): Promise<void> {
const userResponse: Users.GetByUsernameResponse = await client.users.getByUsername('XDevelopers');
const username: string = userResponse.data?.username!;
console.log(username);
}
main();
OAuth 1.0a(ユーザーコンテキスト)
oauth1.ts
Copy
Ask AI
import {
Client,
OAuth1,
type OAuth1Config,
type ClientConfig,
type Users
} from '@xdevplatform/xdk';
const oauth1Config: OAuth1Config = {
apiKey: 'your-api-key',
apiSecret: 'your-api-secret',
accessToken: 'user-access-token',
accessTokenSecret: 'user-access-token-secret'
};
const oauth1: OAuth1 = new OAuth1(oauth1Config);
const config: ClientConfig = {
oauth1: oauth1,
};
const client: Client = new Client(config);
async function main(): Promise<void> {
const response: Users.GetMeResponse = await client.users.getMe();
const me = response.data;
console.log(me);
}
main();
OAuth 2.0(ユーザーコンテキスト)
oauth2.ts
Copy
Ask AI
import {
Client,
OAuth2,
generateCodeVerifier,
generateCodeChallenge,
type OAuth2Config,
type ClientConfig,
type OAuth2Token
} from '@xdevplatform/xdk';
(async (): Promise<void> => {
const oauth2Config: OAuth2Config = {
clientId: 'your-client-id',
clientSecret: 'your-client-secret',
redirectUri: 'https://example.com',
scope: ['tweet.read', 'users.read', 'offline.access'],
};
const oauth2: OAuth2 = new OAuth2(oauth2Config);
const state: string = 'example-state';
const codeVerifier: string = generateCodeVerifier();
const codeChallenge: string = await generateCodeChallenge(codeVerifier);
oauth2.setPkceParameters(codeVerifier, codeChallenge);
const authUrl: string = await oauth2.getAuthorizationUrl(state);
const tokens: OAuth2Token = await oauth2.exchangeCode(authCode, codeVerifier);
const config: ClientConfig = {
accessToken: tokens.access_token,
};
const client: Client = new Client(config);
});
環境変数
Copy
Ask AI
# .env
X_API_BEARER_TOKEN=your-bearer-token
X_API_CLIENT_ID=your-client-id
X_API_CLIENT_SECRET=your-client-secret
env.ts
Copy
Ask AI
import { Client } from '@xdevplatform/xdk';
const client = new Client({ bearerToken: process.env.X_API_BEARER_TOKEN });