这是您可以在node.js / tyscript中使用twitter pin based oauth的方式。< / p>
脚步:
步骤1
在您的项目中安装@vanxh/tw-oauth
npm install @vanxh/tw-oauth
# or
yarn add @vanxh/tw-oauth
步骤2
导入tubauth
import { TwoAuth } from 'twoauth';
步骤3
创建一个新的tureauth实例
const tw = new TwoAuth("API KEY", "API SECRET");
步骤4
获取授权URL
const { token, tokenSecret, authorizationUrl } = await tw.getAuthorizationUrl();
console.log(authorizationUrl);
步骤5
在步骤4中的授权URL上进行身份验证后获得的PIN,以获取访问令牌和访问令牌秘密。
const { accessToken, accessTokenSecret } = await tw.getAccessToken(pin, token, tokenSecret);
完整的用法示例
在github上查看图书馆。
import { TwoAuth } from 'twoauth';
import inquirer from "inquirer";
import open from "open";
const tw = new TwoAuth("API KEY", "API SECRET");
const { token, tokenSecret, authorizationUrl } = await tw.getAuthorizationUrl();
console.log("Please visit the following URL to authorize the app.");
console.log(authorizationUrl);
open(authorizationUrl);
const { pin } = await inquirer.prompt([{
type: "number",
name: "pin",
message: "Paste the PIN here:",
filter: (value) => (value >= 0 && value <= 9_999_999) ? value : Promise.reject(`Invalid PIN: ${value}`)
}]);
const { accessToken, accessTokenSecret } = await tw.getAccessToken(pin, token, tokenSecret);
console.log("Access Token:", accessToken);
console.log("Access Token Secret:", accessTokenSecret);
// Use the access token and access token secret to make API calls to Twitter API.