Skip to content

Instantly share code, notes, and snippets.

@amemiya
Created December 28, 2022 12:45
Show Gist options
  • Save amemiya/14f1614819210b8be5d4afcbf4727ca6 to your computer and use it in GitHub Desktop.
Save amemiya/14f1614819210b8be5d4afcbf4727ca6 to your computer and use it in GitHub Desktop.
Extended OAuth2User in twitter-api-sdk to allow external injection of codeVerifier and codeChallenge.
import {OAuth2User, OAuth2UserOptions} from "twitter-api-sdk/dist/OAuth2User.js";
export default class TwitterOauthService extends OAuth2User {
#options: OAuth2UserOptions
#code_verifier?: string
#code_challenge?: string
/**
* Modified so that codeVerifier and codeChallenge can be set in the constructor.
* Also, codeVerifier and codeChallenge need to be saved in cookies etc., so I made a getter.
* @param tokenObject
* @param codeVerifier
* @param codeChallenge
*/
constructor(tokenObject: any = undefined, codeVerifier: string | undefined = undefined, codeChallenge: string | undefined = undefined) {
const options: OAuth2UserOptions = {
client_id: 'client_id', // Acquired from environment variables, etc.
client_secret: 'client_secret',
callback: "callback_url",
scopes: ["tweet.read", "users.read", "follows.read", "offline.access"],
token: tokenObject,
}
super(options)
const {token, ...defaultOptions} = options
this.#options = defaultOptions
this.#code_verifier = codeVerifier
this.#code_challenge = codeChallenge
this.token = token
}
public getCodeVerifier(): string | undefined {
return this.#code_verifier;
}
public getCodeChallenge(): string | undefined {
return this.#code_challenge
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment