question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Where can I find "YOUR_APP_USER_TOKEN"?

See original GitHub issue

Hi there! I’m new to this, this is the first time I’m trying to use this library. Can you help me to figure out where I can find “YOUR_APP_USER_TOKEN”?

I have copy-pasted the code from the README (with minor changes):

import { TwitterApi } from 'twitter-api-v2'

// Instanciate with desired auth type (here's Bearer v2 auth)
const twitterClient = new TwitterApi(<YOUR_APP_USER_TOKEN>)

// Play with the built in methods
const user = await roClient.v2.userByUsername('plhery')
console.log('user', user)
const tweet = await twitterClient.v1.tweet('Hello, this is a test.')
console.log(tweet)
// You can upload media easily!
const image = await twitterClient.v1.uploadMedia('./format-your-writing-for-readability.jpg')

But I can’t seem to figure out where to get the user token, the only keys I see in my developer portal are these:

screencapture-developer-twitter-en-portal-projects-1470847515079630855-apps-22799089-keys-2021-12-15-14_39_13

Where can I get the user token? What’s the easiest way to obtain it?

(My goal is to post tweets using this api, not just read data, I don’t know if that matters.)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

4reactions
alkihiscommented, Dec 15, 2021

Hi,

So, you’re new to Twitter development? 😄 You now have multiple authentication methods on this API.

  • OAuth2 Bearer Token (shown on example here), is only for app auth. It means you can only read content, not write anything
  • OAuth2 user-context, which are the things related to Client ID and Client Secret, but this has been released to public yesterday and implementation is still a WIP for this lib.
  • OAuth 1.0a user-context token (the historical one), that allows you to post content, but you need to implement a three-legged OAuth flow (example described in Authentication documentation). If you don’t want to implement it right away, you can generate the 4-keys couple “API key”, “API secret”, “access token” and “access token secret” for your account in Twitter Developer portal and use it as it follows:
const client = new TwitterApi({ appKey: 'API key', appSecret: 'API secret', accessToken: 'Access token', accessSecret: 'Access token secret' });

Don’t forget to generate them with read and write permissions.

If you want to implement a login flow “a sign in with Twitter flow”, I highly recommend you to read Twitter documentation and library doc about auth.

0reactions
lumenwritescommented, Dec 16, 2021

Thank you! The second question is resolved, just always using authenticate works, that’s exactly what I want (for some reason I initially assumed that you have to first use authorize and only then use authenticate, but when you just use authenticate it prompts the user to authorize the app when they log in for the first time).

I’m still struggling to obtain the user’s email address though.

I’ve tried both:

const user = await client.currentUser();
console.log(user)

and

const { data: user } = await client.v2.get('users/me');
console.log(user)  

And it returns some info about the user, but no email.

I see that the first link you gave me mentions returning the email if “Request email addresses from users” is checked, and I did check it, but I still don’t see the email in the response. I don’t think I’m using this correctly…

Can you help me to figure out how to obtain the email?

Edit:

I have also tried this:

const credentials = await client.v1.get('account/verify_credentials')

But it returns me a bunch of html with an error, says “Nothing to see here, looks like this page doesn’t exist”.

Edit 2:

Doing this:

const credentials = await client.v1.get('account/verify_credentials.json')

doesn’t give me an error, it gives me a response, but there’s still no email in it…

Edit 3: Yissss!!! That did it:

      const credentials = await client.v1.get('account/verify_credentials.json', {
        include_email: "true", 
        include_entities: "true", 
        skip_status: "true" 
      })

Got the email. Case closed =)

Amazing. Thank you so much for this amazing project, and for your help!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Manage API tokens for your Atlassian account
API tokens can be used with the Jira Cloud and Confluence Cloud REST APIs. If you're using Bitbucket Cloud, see App passwords. Create...
Read more >
User Token authentication Spring MVC RESTful API
User does an initial request to the API to obtain an access token (must provide own credentials - login and password) ¡ Service...
Read more >
User Token REST API - v2 - Sonatype Help
A user token is a pair of a userCode and a passCode unique to the user, which will grant the permissions that are...
Read more >
Access Tokens - Auth0
Describes how access tokens are used in token-based authentication to allow an application to access an API after a user successfully authenticates and ......
Read more >
userToken API parameter - Algolia
The insights taken from this could be used in combination with personalization for example. The user token has to be an alpha-numeric string ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found