error - ApiResponseError: Request failed with code 401 #208
-
My code is similar to https://github.com/fireship-io/gpt3-twitter-bot/blob/main/functions/index.js & https://github.com/alkihis/twitter-api-v2-user-oauth2-flow-example/blob/main/src/routes/callback.ts It's in 3 files: client.tsimport TwitterApi from 'twitter-api-v2'
import { TWITTER_CONFIG } from '../../../lib/config'
export const twitterClient = new TwitterApi({
clientId: TWITTER_CONFIG.clientId,
// clientSecret: TWITTER_CONFIG.clientSecret,
}) generate-auth-link.tsimport { NextApiResponse } from 'next'
import { twitterClient } from './client'
import { TWITTER_CONFIG } from '../../../lib/config'
import { NextIronRequest } from '../../../types/index'
import handler from '../../../server/api-route'
const generateAuthLink = async (req: NextIronRequest, res: NextApiResponse) => {
// Generate an authentication URL
const { state, codeVerifier, url } = twitterClient.generateOAuth2AuthLink(
TWITTER_CONFIG.callbackURL,
{
scope: ['tweet.read', 'offline.access'],
}
)
req.session.twitter = {
state,
codeVerifier,
}
await req.session.save()
// redirect to the authentication URL
res.send({ redirect: url })
}
export default handler().get(generateAuthLink) get-verifier-token.tsimport { SERVER_URL } from './../../../utils/index'
import { NextApiResponse } from 'next'
import TwitterApi from 'twitter-api-v2'
import { twitterClient } from './client'
import { TWITTER_CONFIG } from '../../../lib/config'
import { NextIronRequest } from '../../../types/index'
import handler from '../../../server/api-route'
const getVerifierToken = async (req: NextIronRequest, res: NextApiResponse) => {
const state = req.query.state as string
const code = req.query.code as string
const { state: storedState, codeVerifier } = req.session.twitter
if (storedState !== state) {
return res
.status(400)
.send(
'OAuth token is not known or invalid. Your request may have expired. Please renew the auth process.'
)
}
const {
client: loggedClient,
accessToken,
refreshToken,
expiresIn,
} = await twitterClient.loginWithOAuth2({
code,
codeVerifier,
redirectUri: TWITTER_CONFIG.callbackURL,
})
const {
data: { id: userId, name, username, profile_image_url },
} = await loggedClient.v2.me()
res.send(`
<html>
<body>
<h1>You successfully logged in! closing this window...</h1>
</body>
<script>
window.opener && window.opener.postMessage({ username: '${username}' }, '${SERVER_URL}');
close();
</script>
</html>
`)
}
export default handler().get(getVerifierToken) I'm still getting an error:
Reproduction code on |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Can you give your client type registered in Twitter dev portal? Is it classified as a public client or a confidential client? If this is a confidential client (the excepted one for node.js usage), you need to give your client secret in the constructor (in your exemple, it is commented). |
Beta Was this translation helpful? Give feedback.
-
had to add |
Beta Was this translation helpful? Give feedback.
had to add
clientSecret
to get a 403 forbidden error & wrapawait
intry...catch
to know the error that i hadn't putusers.read
inscope
array. more details here :)