메인 콘텐츠로 건너뛰기

Documentation Index

Fetch the complete documentation index at: https://generaltranslation.mintlify.app/llms.txt

Use this file to discover all available pages before exploring further.

TypeScript SDK는 다양한 사용 사례에 따라 여러 가지 인증 방식을 지원합니다.

Bearer 토큰 (App 전용 인증)

읽기 전용 작업 및 공개 데이터에 액세스할 때:
quickstart.ts
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
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
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);

  // 사용자가 authUrl에 접속해 앱을 승인합니다.
  // 승인 후 사용자는 code 파라미터와 함께 다시 리디렉션됩니다.
  // 콜백 URL(예: 쿼리 매개변수)에서 code를 추출합니다.
  const authCode: string = 'code-from-callback-url'; // OAuth 콜백에서 받은 실제 code로 대체하세요.

  const tokens: OAuth2Token = await oauth2.exchangeCode(authCode, codeVerifier);

  const config: ClientConfig = {
    accessToken: tokens.access_token,
  };

  const client: Client = new Client(config);
});

환경 변수

민감한 자격 증명 정보는 환경 변수에 저장하세요.
# .env
X_API_BEARER_TOKEN=your-bearer-token
X_API_CLIENT_ID=your-client-id
X_API_CLIENT_SECRET=your-client-secret
env.ts
import { Client } from '@xdevplatform/xdk';

const client = new Client({ bearerToken: process.env.X_API_BEARER_TOKEN });
JavaScript/TypeScript XDK를 사용하는 보다 자세한 코드 예시는 코드 샘플 GitHub 리포지토리를 참고하세요.