Auth0 Home Blog Docs

Twitter auth get token and secret token

im using auth0 to get authentication for twitter, im using react native and i want to use twitter as login,
this is my code.

_loginWithAuth0Twitter = async () => {
    const redirectUrl = AuthSession.getRedirectUrl();
    const result = await AuthSession.startAsync({
        authUrl: `${auth0Domain}/authorize` + toQueryString({
            connection: 'twitter',
            client_id: auth0ClientId,
            response_type: 'token',
            scope: 'openid',
            redirect_uri: redirectUrl,
        }),
    });

after request auth give me this result

Object {
  "errorCode": undefined,
  "params": Object {
    "access_token": "8uDhJTvWFxpr6GpTfioXp_8wCtqfwDsW",
    "exp://127.0.0.1:19000/--/expo-auth-session": "",
    "expires_in": "7200",
    "scope": "openid",
    "token_type": "Bearer",
  },
  "type": "success",
  "url": "exp://127.0.0.1:19000/--/expo-auth-session#access_token=8uDhJTvWFxpr6GpTfioXp_8wCtqfwDsW&scope=openid&expires_in=7200&token_type=Bearer",
}

i try to set rules

Get email address from Twitter

function (user, context, callback) {
  // additional request below is specific to Twitter
  if (context.connectionStrategy !== 'twitter') {
    return callback(null, user, context);
  }

  const oauth = require('oauth-sign');
  const uuid = require('uuid');

  const url = 'https://api.twitter.com/1.1/account/verify_credentials.json';
  const consumerKey = configuration.TWITTER_CONSUMER_KEY;
  const consumerSecretKey = configuration.TWITTER_CONSUMER_SECRET_KEY;

  const twitterIdentity = _.find(user.identities, { connection: 'twitter' });
  const oauthToken = twitterIdentity.access_token;
  const oauthTokenSecret = twitterIdentity.access_token_secret;

  const timestamp = Date.now() / 1000;
  const nonce = uuid.v4().replace(/-/g, '');

  const params = {
    oauth_consumer_key: consumerKey,
    oauth_nonce: nonce,
    oauth_signature_method: 'HMAC-SHA1',
    oauth_timestamp: timestamp,
    oauth_token: oauthToken,
    oauth_version: '1.0',
    oauth_callback:'https://pembersih.auth0.com/login/callback'
  };

  params.oauth_signature = oauth.hmacsign('POST', 
                                          url, 
                                          params, 
                                          consumerSecretKey, 
                                          oauthToken);

  const auth = Object.keys(params).sort().map(function (k) {
    return k + '="' + oauth.rfc3986(params[k]) + '"';
  }).join(', ');

  request.post(url, {
    headers: {
      'Authorization': 'OAuth ' + auth
    },
    json: true
  }, (err, resp, body) => {
    if (resp.statusCode !== 200) {
      return callback(new Error('Error retrieving email from twitter: ' + body || err));
    }
  });
}

then i get this error

Object {
  "errorCode": undefined,
  "params": Object {
    "error": "access_denied",
    "error_description": "Error retrieving email from twitter: [object Object]",
    "exp://127.0.0.1:19000/--/expo-auth-session": "",
  },
  "type": "success",
  "url": "exp://127.0.0.1:19000/--/expo-auth-session#error=access_denied&error_description=Error%20retrieving%20email%20from%20twitter%3A%20%5Bobject%20Object%5D",
}

how can i get user token and secret token so that i can use twitter API ?

Hey there @zulfiqarlaili!

Can you try using our quickstart on using react-native, it should guide you step by step to what you want to achieve:

Let me know if you have any questions!

thanks for the help but currently im using expo on top of react native, so i dont have access to android and ios folder just like the tutorial suggest. im using expo for simplicity and dont want to manage those platform file.

currently my code run and im able to connect with Auth0 using browser just fine but the problem is with the respond that i receive. it only give me access_token what i do need is token and token_secret from twitter, because twitter api still using Auth 1.0