2017-08-07 581 views
1

我試圖實施SingIn使用Twitter在我的node.js app。首先我打電話給https://api.twitter.com/oauth/request_token,twitter給我oauth_token,然後打電話給https://api.twitter.com/oauth/authenticate?oauth_token=TWITTER_OAUTH_TOKEN打開一個新窗口,顯示用戶可以接受這個應用程序的登錄信息。用戶接受後,twitter會撥打callback url並撥打oauth_tokenoauth_verifier。現在我打電話https://api.twitter.com/oauth/access_token得到access_token。而我得到access_token像爲給定的格式使用twitter登錄

{ 
    oauth_token: 'NEW_TWITTER_OAUTH_TOKEN', 
    oauth_token_secret: 'NEW_TWITTER_OAUTH_TOKEN_SECRET', 
    user_id: 'TWITTER_USER_ID', 
    screen_name: 'TWITTER_SCREEN_NAME', 
    x_auth_expires: '0' 
} 

現在我想要獲得更多的用戶信息,如user_email, user_pic etc。我打電話以下API https://api.twitter.com/1.1/account/verify_credentials.json有下列參數:

let hmac = crypto.createHmac('sha1', 'thisIsTest'); 
hmac.update(uuid.v4()); 
let hash = hmac.digest('hex'); 

var profileOauth = { 
    oauth_consumer_key: 'twitter_consumer_key', 
    oauth_nonce: uuid.v4(), 
    oauth_signature_method:'HMAC-SHA1', 
    oauth_signature: hash, 
    oauth_timestamp: Date.parse(new Date()), 
    oauth_version: '1.0', 
    oauth_token: 'twitter_accessToken_oauth_token' 
    }; 

    request.get({ 
    url: 'https://api.twitter.com/1.1/account/verify_credentials.json', 
    // qs: { include_email: true }, 
    oauth: profileOauth, 
    json: true 
    }, function(err2, resp, profile) { 

    console.log("profile 1 : ", profile); 
    res.json(profile); 

    }); 

而且我發現了以下錯誤:

profile 1 : { errors: [ { code: 215, message: 'Bad Authentication data.' } ] } 

任何建議?提前致謝。

回答

0
var profileOauth = { 
oauth_consumer_key: 'twitter_consumer_key', 
oauth_nonce: uuid.v4(), 
oauth_signature_method:'HMAC-SHA1', 
oauth_signature: hash, 
oauth_timestamp: Date.parse(new Date()), 
oauth_version: '1.0', 
oauth_token: 'twitter_accessToken_oauth_token' 
}; 

嘗試用這種

var profileOauth = { 
oauth_consumer_key: 'twitter_consumer_key', 
oauth_consumer_key_secret: 'twitter_consumer_key_secret', //add consumer_key secret 
oauth_nonce: uuid.v4(), 
oauth_signature_method:'HMAC-SHA1', 
oauth_signature: hash, 
oauth_timestamp: Date.parse(new Date()), 
oauth_version: '1.0', 
oauth_token: 'twitter_accessToken_oauth_token', 
oauth_token_secret: 'oauth_token_secret' // add oauth_token_secret 
}; 

我想你忘記在你的參數列表應用用戶端密鑰和用戶的OAuth令牌密鑰更換上面的代碼。