简体   繁体   English

使用JavaScript在Twitter上共享base64图像

[英]Sharing a base64 image on Twitter using javascript

I spent hours and hours to resolve this problem... but couldn't get any tutorials or examples for this. 我花了几个小时来解决这个问题...但是却没有任何教程或示例。 Tried to implement something but confronted terrible errors. 试图实施某些措施,但遇到了可怕的错误。

I'm working on client slide and it seems "POST statuses/update_with_media" in REST Api of Twitter is especially designed to share image data url, but originally, Twitter documentation is very poor and has no example codes for this. 我正在处理客户端幻灯片,Twitter的REST Api中的“ POST statuss / update_with_media”似乎专门用于共享图像数据url,但是最初,Twitter文档非常差,并且没有示例代码。 I am even not sure how to give user_secret, user_token, consumer_key and consumer_secret in ajax request. 我什至不确定如何在ajax请求中提供user_secret,user_token,consumer_key和consumer_secret。 This is what I've done. 这就是我所做的。

        var fd = new FormData();
        fd.append("oauth_consumer_key", "knC68njKLjXrRljSxhE3uywsi");
        fd.append("status", "");
        fd.append("media[]", base64_string);

        $.ajax({
            url:"https://api.twitter.com/1.1/statuses/update_with_media.json",
            type:"POST",
            data:fd,
            processData:false,
            contentType: "multipart/form-data",
            cache:false,
            success:function(data){
                alert('Post was published.');
            },
            error:function(shr,status,data){
                alert('Post was not published.');
            },
            complete:function(){
            }
        });

I know this is a bad try but that was all I could do. 我知道这是一个不好的尝试,但这就是我所能做的。 And this gives below error. 这给出了以下错误。

XMLHttpRequest cannot load https://api.twitter.com/1.1/statuses/update_with_media.json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.168.0.101' is therefore not allowed access. 

I'm running this on local php server. 我在本地php服务器上运行它。 Any examples or tutorials for this will be appreciated. 任何示例或教程,将不胜感激。

Thanks. 谢谢。

I do not know about the specifics of this API call but, from a first look, you can't make an ajax call to a server different than the one your page originated from. 我不知道此API调用的具体细节,但是,乍一看,您无法对不同于您页面所源自的服务器的ajax调用。 Thus, you cannot make an ajax call to the twitter api, unless twitter supports CORS . 因此,除非twitter支持CORS ,否则您无法对twitter api进行ajax调用。 The error message you get indicates exactly this. 您收到的错误消息恰恰表明了这一点。 The only way to solve this is by making the twitter api call from your server, and then have your page make the ajax call to your server. 解决此问题的唯一方法是从服务器进行twitter api调用,然后让页面对服务器进行ajax调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM