使用Auth0 for Twitter获取令牌和秘密令牌

时间:2019-03-09 04:24:39

标签: twitter token auth0 twitter-login

即时通讯使用auth0获得Twitter身份验证,即时通讯使用react native,我想使用twitter作为登录名, 这是我的代码。

_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,
        }),
    });

请求授权后给我这个结果

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",
}

我只获得acess_token,访问令牌没有什么可做的,因为Twitter仍在使用auth 1.0

我尝试设置规则

  

从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));
    }
  });
}

然后我得到这个错误

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",
}

如何获取用户令牌秘密令牌,以便我可以使用Twitter API?

0 个答案:

没有答案