# Twitter (X) (/docs/authentication/twitter)

Twitter 提供商的设置和使用说明。



<Steps>
  <Step>
    ### 获取您的 Twitter 凭证 [#获取您的-twitter-凭证]

    从 [Twitter Developer Portal](https://developer.twitter.com/en/portal/dashboard) 获取您的 Twitter 凭证。

    请确保将重定向 URL 设置为 `http://localhost:3000/api/auth/callback/twitter`，以用于本地开发。在生产环境中，应将其设置为应用程序的 URL。如果更改了认证路由的基本路径，请相应地更新重定向 URL。

    <Callout type="info">
      Twitter API v2 现在支持检索电子邮件地址。在配置 Twitter 应用时，请务必请求 `user.email` 范围以启用此功能。
    </Callout>
  </Step>

  <Step>
    ### 配置提供商 [#配置提供商]

    要配置提供商，您需要导入提供商并将其传递给认证实例的 `socialProviders` 选项。

    ```ts title="auth.ts"  
    import { betterAuth } from "better-auth" 

    export const auth = betterAuth({
        socialProviders: {
            twitter: { // [!code highlight]
                clientId: process.env.TWITTER_CLIENT_ID as string, // [!code highlight]
                clientSecret: process.env.TWITTER_CLIENT_SECRET as string, // [!code highlight]
            }, // [!code highlight]
        },
    })
    ```
  </Step>

  <Step>
    ### 使用 Twitter 登录 [#使用-twitter-登录]

    要使用 Twitter 登录，可以使用客户端提供的 `signIn.social` 函数。`signIn` 函数接收一个包含以下属性的对象：

    * `provider`：要使用的提供商，应设置为 `twitter`。

    ```ts title="auth-client.ts"  
    import { createAuthClient } from "better-auth/client"
    const authClient =  createAuthClient()

    const signIn = async () => {
        const data = await authClient.signIn.social({
            provider: "twitter"
        })
    }
    ```
  </Step>
</Steps>

