Skip to content

Can't post Tweet using Parse server, works fine on Parse.com #2062

Closed
@mmahmood

Description

@mmahmood

Issue Description

I am posting Tweets via the REST API, and I am getting error "Could not authenticate you". The problem is that my code has been working fine, and still works fine on Parse.com. Now I am testing on Parse server on my localhost and also on the one on AWS. Both give me the same error. I have checked all the keys and secrets.

  • There are no spaces, and all have code + keys seems to be correct.
  • I have the .js files for oauth downloaded and included in the code, so that should also be the same on all deployments.
  • I have tried setting my server time to GMT+0, which is the one on Parse.com.
  • I have reordered my params to sort alphabetically, but it didn't work.

Here's what I'm doing: https://github.com/sreejithbnaick/Twitter-OAuth-1.1-Signature-Generator-js
This is working perfectly fine on Parse.com.

Code

var oauth1Header = self.twitterOAuth1Header(urlLink, post, config, userAccessToken.get('twitterAccessTokenSecret'),
userAccessToken.get('twitterAccessToken'), consumerSecret, httpMethod);

return Parse.Cloud.httpRequest({
    method: httpMethod,
    url: urlLink,
    headers: oauth1Header,
    body: "status="+status
});

exports.twitterOAuth1Header = function(urlLink, post, config, tokenSecret, oauth_token, consumerSecret, httpMethod){
// Source: https://github.com/sreejithbnaick/Twitter-OAuth-1.1-Signature-Generator-js

var oauth_consumer_key = config.get('twitterConfig').oauthConsumerKey;
var nonce = oauth.nonce(32);
var ts = Math.floor(new Date().getTime() / 1000);
var timestamp = ts.toString();

var enc_oauth_consumer_key = oauth.percentEncode(oauth_consumer_key);   
var enc_nonce = oauth.percentEncode(nonce);
var enc_oauth_token = oauth.percentEncode(oauth_token);

var accessor = {
    "consumerSecret": consumerSecret,
    "tokenSecret": tokenSecret
};

var params = {
    // "status":postSummary,
    "oauth_version": "1.0",
    "oauth_consumer_key": oauth_consumer_key,
    "oauth_token": oauth_token,
    "oauth_timestamp": timestamp,
    "oauth_nonce": nonce,
    "oauth_signature_method": "HMAC-SHA1"
};

if(post.length > 0){
    params["status"] = post.substring(0,140);
}
var message = {
    "method": httpMethod,
    "action": urlLink,
    "parameters": params
};

//Creating signature
oauth.SignatureMethod.sign(message, accessor);
var normPar = oauth.SignatureMethod.normalizeParameters(message.parameters);
var baseString = oauth.SignatureMethod.getBaseString(message);
var sig = oauth.getParameter(message.parameters, "oauth_signature") + "=";
var enc_signature = oauth.percentEncode(sig); //finally you got oauth signature

return {"Authorization": 'OAuth oauth_consumer_key="'+enc_oauth_consumer_key+'", oauth_nonce=' + enc_nonce + ', oauth_signature=' + enc_signature + ', oauth_signature_method="HMAC-SHA1", oauth_timestamp=' + timestamp + ',oauth_token="'+enc_oauth_token+'", oauth_version="1.0"'};

}

Expected Results

Successful Tweet.

Actual Outcome

Error from Twitter
{ "code": 32, message": "Could not authenticate you."}

Environment Setup

  • Server
    • parse-server version: 2.2.10
    • Operating System: Windows 10
    • Localhost or remote server? AWS, and localhost
  • Database
    • MongoDB version: 3.0.7
    • Localhost or remote server? mLab

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions