json 手动将Twitter应用程序授权给Twitter帐户

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了json 手动将Twitter应用程序授权给Twitter帐户相关的知识,希望对你有一定的参考价值。

So you're writting a twitterbot and you need to authorize your application to post to the account. You need an access token and secret for the account you're posting to. If the posting account is the same account that owns the application, no problem, you just push the button on your application's settings page to make the keys. But if you want to post to a different twitter account, there's no UI on apps.twitter.com to authorize it. So I made this bare-minimum node server to run through the authorization process. There's probably a much better way to do this, so please let me know what that way is! 

# ignore this and go down to the comments for better solutions

* You'll need a server with node.js!
* Make sure your application has a callback URL specified in its settings page, even if it's just a placeholder. If there's nothing in the callback URL slot, this method of authorization won't work.
* In authorize.js, fill in your application's consumer key and secret, and the domain on which you'll be running this mini-server (I think it'll work with 'localhost' if you're running it on your laptop, but I haven't tested it that way).
* Install the application (npm install)
* Run the server (node authorize.js)
* In a browser, make sure you're logged in to the account that you want to authorize, and then visit this server in the browser (e.g. http://localhost:3456)
* Click "authenticate" and you should be sent to the Twitter app authorization page
* When you authorize the app, you should see a page displaying your new access token and secret. Keep those safe!

If it doesn't work for you, I probably can't help you, but someone who understands this system better than I do will probably make a version of this process that works better.
var express = require('express');
var app = express();

var twitterAPI = require('node-twitter-api');
var twitter = new twitterAPI({
	// application keys for your application
    consumerKey: '...',
    consumerSecret: '...',
    callback: 'http://your domain or localhost:3456/auth'
});

var requestToken, requestTokenSecret;

// following instructions from https://www.npmjs.org/package/node-twitter-api


app.get('/', function(req, res){
	twitter.getRequestToken(function(error, _requestToken, _requestTokenSecret, results){
		if (error) {
			res.send(error);
		} else {
			//store token and tokenSecret somewhere, you'll need them later; redirect user
			requestToken = _requestToken;
			requestTokenSecret = _requestTokenSecret;
			
			res.send('<a href="' + twitter.getAuthUrl(requestToken) + '">authenticate</a>');
		}
	});
});

app.get('/auth', function(req, res) {
	
	twitter.getAccessToken(requestToken, requestTokenSecret, req.query.oauth_verifier, function(error, accessToken, accessTokenSecret, results) {
	    if (error) {
	        res.send(error);
	    } else {
	        //store accessToken and accessTokenSecret somewhere (associated to the user)
	        res.send('accessToken: <b>' + accessToken + '</b><br/>accessTokenSecret: <b>' + accessTokenSecret + '</b>');
	    }
	});	

});




app.listen(3456);
{
  "name": "twitter authorizing thingy",
  "version": "1.0.0",
  "description": "",
  "main": "authorize.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.10.1",
    "node-twitter-api": "^1.5.0"
  }
}

以上是关于json 手动将Twitter应用程序授权给Twitter帐户的主要内容,如果未能解决你的问题,请参考以下文章

使用 Cordova 和 Angularjs 登录 Twitter:401 未经授权的错误

TwitteR、ROAuth 和 Windows:注册成功,但证书验证失败

使用 Fabric SDK 从 Twitter 授权中取消时出现授权错误

如何在 php 中创建类似 twitter 的 search.json

未使用 Twitter Bootstrap 修复导航栏内容时居中?

Twitter关注者图片列表