Authentication failure with Twitter API

I am trying to use the Twitter API with the following source code.
In “Cocos Creator v1.0.3”, I get the response as intended.

However, in “CocosCreator v1.1.0 to v1.6.0”, authentication failed and “” code “: 215,” message “:” Bad Authentication data. “” Is responded.

var xhr = cc.loader.getXMLHttpRequest();
xhr.open("GET", requestURL, true);
xhr.setRequestHeader("ContentType", "application/x-www-form-urlencoded");
xhr.setRequestHeader("Authorization", authHeader);
xhr.onreadystatechange = function () {
    console.log(xhr.responseText);
};
xhr.send();

The contents of “requestURL” and “authHeader” in the source code are correct. Because I sent by UnityWebRequest.Get () on Unity and got the correct response from “Twitter API”.

In “CocosCreator v1.1.0 to v1.6.0”, why authentication failed?
How can I improve my source code?

I think you need you keys/tokens in there

Example from https://stackoverflow.com/questions/14681182/how-to-call-twitter-v1-1-api-in-javascript-using-ajax

xhr.setRequestHeader('Authorization','OAuth oauth_consumer_key="HdFdA3C3pzTBzbHvPMPw", oauth_nonce="4148fa6e3dca3c3d22a8315dfb4ea5bb", oauth_signature="uDZP2scUz6FUKwFie4FtCtJfdNE%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp= "1359955650", oauth_token, "1127121421-aPHZHQ5BCUoqfHER2UYhQYUEm0zPEMr9xJYizXl", oauth_version="1.0"');

Thanks for your reply.
In the last source code, the requestURL and authHeader I sent to search tweets are as follows.

requestURL = "https://api.twitter.com/1.1/search/tweets.json?count=1&q=cocos2d";
authHeader = "OAuth realm=\"Twitter API\",count=\"1\",oauth_consumer_key=\"******\",oauth_nonce=\"30757D0B\",oauth_signature_method=\"HMAC-SHA1\",oauth_timestamp=\"1501485209\",oauth_token=\"******\",oauth_version=\"1.0\",q=\"cocos2d\",oauth_signature=\"******\"";

In the part of ******, necessary key / token is inputted respectively.

In Cocos Creator v1.0.3, I succeeded in searching tweets.
In Cocos Creator v1.1.0 to v1.6.0, I failed to search (authenticate).
The source code is the same.

Why is the result changed depending on the version difference?
I would like to use the Twitter API with the latest version of Cocos Creator.

I am using Mac version of Cocos Creator.

This is additional information.

When executing the above source code with windows version of Cocos Creator v1.5.1, we succeeded in searching tweets.

However, the Mac version of Cocos Creator v1.5.1 still failed authentication.

Even if you canceled the Mac’s firewall it failed.
I would like to use the Mac version of Cocos Creator.

Is there anyone who can confirm the same thing?
Please help someone.

Instead of using CCs cc.loader.getXMLHttpRequest have you tried just using a normal new XMLHttpRequest?

maybe there’s a bug in CCs version.

Thanks for your reply.

I tried ‘new XMLHttpRequest’, but the result was a failure.

Console.log (xhr.responseText);
 ==> {"errors": [{"code": 215, "message": "Bad Authentication data."}]}

It is the execution result with Cocos Creator’s simulator.

Can you use the Twitter API with Mac version of Cocos Creator v1.5.1?

It’s a bad authentication error. https://stackoverflow.com/questions/17143985/twitter-api-error-215

I haven’t tried using the twitter API in CC myself.

I want to know if this phenomenon can be reproduced by other people.
It helps me to determine if the cause of this problem is in my environment or in Cocos Creator itself.

Why don’t you try in a browser, not using CC?

When you access the Twitter API from the browser, the Twitter API can not be used because cross domain problem occurs.

Which platform did you test on?
Web, Native(iOS, Android, Mac, Win32)?

Hi, dumganhar

In the following version of Cocos Creator, I selected “Simulator” and executed it with “preview button”.
· “Mac version of Cocos Creator v1.0.3 or earlier” successfully searched tweets.
· “Windows version of Cocos Creator v1.5.1” successfully searched tweets.
· In “Mac version of Cocos Creator v1.1.0 to v1.6.0”, Twitter API authentication failed.

And in the Mac version of Cocos Creator v1.5.1, I built it for Native (Mac) and executed it, but the authentication failed as same as the simulator.

Could you send me a demo which reproduces this bug? I will try to figure out what happens on Mac version.

I ran “the source code written in the first post” in onload () of Hello World Project.
“authHeader” not be disclosed because it contains personal information.

Sorry for my late reply. What about the status of this issue? Did you fix it?

So how could I to reproduce the problem?