Error 215: Bad Authentication Data

nodejs
javascript

#1

I am receiving a Bad Authentication Data error when I try to call to users/show. I am confused as to why because I feel as though I have followed the documentation perfectly.

This is not an issue with either the Token or Token Secret, as I have successfully acquired them previously.

// Users Route
usersRouter.get(
	'/show',
	(req, res, next) => {
		req.httpMethod = 'GET';
		req.baseURL = 'https://api.twitter.com/1.1/users/show.json';
		next();
	},
	oAuthController,
	(req, res) => {
		axios
			.get(req.baseURL, {
				headers: {
					Authorization: req.headerStr,
				},
			})
			.then(response => console.log(response))
			.catch(err => console.log(err.response.data));
	},
);

// oAuthController
const oAuthController = (req, res, next) => {
	User.findById(req.query.user_id)
		.then(user => {
			const httpMethod = req.httpMethod;
			const baseUrl = req.baseURL;

			const oauth_consumer_key = process.env.TWITTER_KEY;
			const oauth_nonce = crypto
				.randomBytes(Math.ceil((32 * 3) / 4))
				.toString('base64')
				.slice(0, 32)
				.replace(/\+/g, '0')
				.replace(/\//g, '0');
			const oauth_signature_method = 'HMAC_SHA1';
			const oauth_timestamp = Math.round(new Date().getTime() / 1000);
			const oauth_token = user.token;
			const oauth_version = '1.0';
			const user_id = user._id;

			const paramArr = [
				{ key: 'oauth_consumer_key', value: oauth_consumer_key },
				{ key: 'oauth_nonce', value: oauth_nonce },
				{ key: 'oauth_signature_method', value: oauth_signature_method },
				{ key: 'oauth_timestamp', value: oauth_timestamp },
				{ key: 'oauth_token', value: oauth_token },
				{ key: 'oauth_version', value: oauth_version },
				{ key: 'user_id', value: user_id },
			];

			let paramStr = '';

			paramArr.map((param, idx) => {
				const key = encodeURIComponent(param.key);
				const value = encodeURIComponent(param.value);

				paramStr += `${key}=${value}`;

				if (paramArr[idx + 1]) {
					paramStr += '&';
				}
			});

			const signStr = `${httpMethod}&${encodeURIComponent(
				baseUrl,
			)}&${encodeURIComponent(paramStr)}`;

			const consumer_secret = process.env.TWITTER_SECRET;
			const token_secret = user.tokenSecret;

			const signKey = `${encodeURIComponent(
				consumer_secret,
			)}&${encodeURIComponent(token_secret)}`;

			const oauth_signature = crypto
				.createHmac('sha1', signKey)
				.update(signStr)
				.digest('base64');

			const headerArr = [
				...paramArr,
				{ key: 'oauth_signature', value: oauth_signature },
			];

			let headerStr = 'OAuth ';

			headerArr.map((param, idx) => {
				const key = encodeURIComponent(param.key);
				const value = encodeURIComponent(param.value);

				headerStr += `${key}="${value}"`;

				if (headerArr[idx + 1]) {
					headerStr += ', ';
				}
			});

			req.headerStr = headerStr;

			next();
		})
		.catch(console.error);
};

closed #2

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.