Calling Twitter's API with jsonp dataType will throw a 400 error

和自甴很熟 提交于 2019-12-12 02:19:59

问题


When trying to perform an API call to Twitter, I noticed I must use "jsonp" to bypass the cross-domain problem. Using "json" dataType will throw a 400 "Control-Allow-Origin" error.

On the other hand, when I'm switching to jsonp I'm always receiving a 400 error with the following message:

jquery.min.js:4 GET https://api.twitter.com/1.1/search/tweets.json?callback=aloha&q=dog

Any ideas on what I'm doing wrong here and how to get it fixed?

<script type="text/javascript">
    function aloha(result) {
        debugger;
    };
    var twitter_call = $.getJSON({
             type: 'GET',
             contentType: 'application/javascript',
             dataType: 'jsonp',
             cache: true,
             jsonpCallback: 'aloha',
             url: 'https://api.twitter.com/1.1/search/tweets.json',
             crossDomain: true,
             headers: {
                "Authorization": "Bearer MY_BEARER_TOKEN"
             },
             data: {
                "q":"dog"
             }
        });
</script>

回答1:


This is officially unsupported by Twitter as mentioned by their team on this topic I raised on their development forum:

https://twittercommunity.com/t/error-401-problems-with-jsonp-token-with-twitter-rest-api-ajax/73618

Any workarounds found for this topic will be posted directly in that link.



来源:https://stackoverflow.com/questions/39338836/calling-twitters-api-with-jsonp-datatype-will-throw-a-400-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!