Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mmahmood opened this issue Jun 14, 2016 · 6 comments
Closed

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

mmahmood opened this issue Jun 14, 2016 · 6 comments

Comments

@mmahmood
Copy link

mmahmood commented Jun 14, 2016

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
@sprabs
Copy link

sprabs commented Jun 15, 2016

Hmm... I am not seeing this particular issue, but are you having any other issues with the REST API? We are noticing that when we are writing and reading data from our mLab server for some period of time, we see high latency and errors (even though the RPS is around 4 from the external processes using REST API).

Also, did you have to make any changes (other than API URI endpoint change) in your server code?

@mmahmood
Copy link
Author

mmahmood commented Jun 15, 2016

For this particular use case, I did not have to even change the API URI endpoint in the server code.

I am using three different REST calls with the same code that generates the authentication header:
GET https://api.twitter.com/1.1/account/verify_credentials.json is working.
But POST https://api.twitter.com/1.1/statuses/update.json, and https://api.twitter.com/1.1/friendships/lookup.json, are not working.

All three work fine on Parse.com hosted code.

@sprabs
Copy link

sprabs commented Jun 15, 2016

@mmahmood How about with read/writes of classes? Are you noticing any issues there with performance? We are having high latency when we are doing many reads/writes (even when equivalent parse.com RPS ~ 4 requests/sec) through REST API. Not sure what we are doing wrong/if there are some issues with REST API, in general, through parse-server.

Yes, the problems we are facing... all work fine on the parse.com hosted code, as well.

@mmahmood
Copy link
Author

In my case, requests are being triggered manually and are probably 10/hour since I'm the only one testing at the moment.

So basically what you're saying is that you can't help me in this? :(

@sprabs
Copy link

sprabs commented Jun 16, 2016

Did you add keys to the environment variables through the UI?

@mmahmood
Copy link
Author

Figured out the issue. In Parse.com, by default, Cloud Code httpRequests POST data with Content-Type application/x-www-url-formencoded.

I explicitly added this content type to my header, and things are working now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants