使用Node.js将照片发布到Tumblr

时间:2014-11-16 17:53:52

标签: javascript node.js base64 tumblr

我正试图通过带有Tumblr.js的Node.js将画布生成的图像(如“data:image / png; base64,iVBORw0KGgoAAAANSUh ...”)发布到Tumblr,但我真的很难过

我在Tumblr上注册了我的应用程序,我有我的tumblrConsumerKey,我的tumblrConsumerSecret,我设法让Oauth工作并发布文本帖子没有问题。我的代码工作正常,但是当我尝试发布图像时,一切都崩溃了。

API文档说我可以使用数据方法发布,并且图像应该是数组(URL编码的二进制内容)。一开始我认为我的“data:image / png; base64 ......”工作得很好,但事实并非如此。然后我尝试对URI进行URI编码,然后对URI进行编码并对其进行二进制转换。都没有奏效。然后我发现一个人专门为Tumblr遇到了writing a js convertor的麻烦,但它也没有用。而现在我真的陷入了困境,我对如何转换我的图像有了零的想法。

有人可以帮助我或指出正确的方向吗?这是我的代码:

var express = require('express');
var oauth = require('oauth');
var http = require('http');
var tumblr = require('tumblr.js');
 
var app = express();
 
app.set('port', process.env.PORT || 3000);
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.errorHandler());
 
/**
 * These four variables will be needed to use tumblr.js
 */
var tumblrConsumerKey = "mytumblrConsumerKey",
    tumblrConsumerSecret = "mytumblrConsumerSecret",
    tumblrOauthAccessToken = undefined,
    tumblrOauthAccessTokenSecret = undefined,
    // Temporary request tokens
    oauthRequestToken,
    oauthRequestTokenSecret;
 
 
/**
 * This object will be used for OAuth
 **/
var consumer = new oauth.OAuth(
    "http://www.tumblr.com/oauth/request_token",
    "http://www.tumblr.com/oauth/access_token",
    tumblrConsumerKey,
    tumblrConsumerSecret,
    "1.0A",
    "http://localhost:3000/auth/callback",
    "HMAC-SHA1"
);
 
app.get('/', function (req, res) {
    if (!tumblrOauthAccessToken || !tumblrOauthAccessTokenSecret) {
        res.redirect('/auth/request');
    }
    res.send('You are logged in and ready to go');
});
 
 
app.get('/auth/request', function (req, res) {
    consumer.getOAuthRequestToken(function(error, oauthToken, oauthTokenSecret){
        if (error) {
            res.send("Error getting OAuth request token: " + error, 500);
        } else {
            oauthRequestToken = oauthToken,
            oauthRequestTokenSecret = oauthTokenSecret;
 
            res.redirect("http://www.tumblr.com/oauth/authorize?oauth_token=" + oauthRequestToken);
        }
    });
});
 
 
app.get('/auth/callback', function (req, res) {
    consumer.getOAuthAccessToken(oauthRequestToken, oauthRequestTokenSecret, req.query.oauth_verifier, function(error, _oauthAccessToken, _oauthAccessTokenSecret) {
        if (error) {
            res.send("Error getting OAuth access token: " + error, 500);
        } else {
            tumblrOauthAccessToken = _oauthAccessToken;
            tumblrOauthAccessTokenSecret = _oauthAccessTokenSecret;
 
            res.send("You are signed in. <a href='/test'>Post the image</a>");
              
        }
    });
});
 
 
app.get('/test', function (req, res) {
    if (!tumblrOauthAccessToken || !tumblrOauthAccessTokenSecret) {
        res.redirect('/auth/request');
    }
 
    var client = tumblr.createClient({
        consumer_key: tumblrConsumerKey,
        consumer_secret: tumblrConsumerSecret,
        token: tumblrOauthAccessToken,
        token_secret: tumblrOauthAccessTokenSecret
    });
    
    /**
     * Post your stuff here
     **/
     
     var img = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==";
     
     
	client.photo('myTumblrName', { source: img }, function (err, resp) {
	 console.log(err);
	});
	    
});
 
 
http.createServer(app).listen(app.get('port'), function () {
    console.log('Express server listening on port ' + app.get('port'));
});

0 个答案:

没有答案