Created
September 11, 2022 20:10
-
-
Save manzt/d6cbf7f901a6e7b68c145d3820fbed7b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as oauth from "https://raw.githubusercontent.com/snsinfu/deno-oauth-1.0a/main/mod.ts"; | |
class OAuthClient extends oauth.OAuthClient { | |
fetch( | |
input: RequestInfo | URL, | |
init: RequestInit & { token?: oauth.Token }, | |
): Promise<Response> { | |
let href = typeof input === "string" | |
? input | |
: input instanceof URL | |
? input.href | |
: input.url; | |
let method = init.method ?? "GET"; | |
let headers = new Headers(init.headers); | |
if (init.token) { | |
headers.set( | |
"Authorization", | |
oauth.toAuthHeader( | |
this.sign(method, href, { token: init.token }), | |
), | |
); | |
} | |
return fetch(input, { ...init, method, headers }); | |
} | |
} | |
export class TwitterClient extends OAuthClient { | |
token?: oauth.Token; | |
constructor(options: Partial<oauth.ClientOptions> & { token?: oauth.Token } = {}) { | |
let consumer = options.consumer ?? { | |
key: Deno.env.get("TWITTER_API_KEY"), | |
secret: Deno.env.get("TWITTER_API_SECRET"), | |
}; | |
super({ consumer, signature: oauth.HMAC_SHA1 }); | |
this.token = options.token ?? { | |
key: Deno.env.get("TWITTER_ACCESS_TOKEN"), | |
secret: Deno.env.get("TWITTER_ACCESS_TOKEN_SECRET"), | |
}; | |
} | |
fetch(route: string, init: RequestInit & { token?: oauth.Token }) { | |
let token = init.token ?? this.token; | |
return super.fetch(new URL(route, "https://api.twitter.com"), { | |
...init, | |
token, | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.