Permalink
Browse files

trying to get oauth testing working

  • Loading branch information...
1 parent 92f56dc commit bf00d7db5148b09e92e1c3163d4cef5259b65a55 @mckelvey committed Jun 23, 2011
View
@@ -450,13 +450,13 @@ To obtain a user url for the link to Instagram, use the authorization_url method
#### Ask for an Access Token
-The example below uses Express to specify a route to respond to the user's return from Instagram. It will pass the access_token and user object returned to a provided complete function. Your respond function should handle the server response (passed as a parameter) as shown below. Versions of instagram-node-lib prior to 0.0.6 accepted the redirect parameter (now deprecated).
+The example below uses Express to specify a route to respond to the user's return from Instagram. It will pass the access_token and user object returned to a provided complete function. Your complete function should handle the server response (passed as a parameter) *or* include a redirect parameter for simple redirects. Be advised however, due to the event model of node.js, your users may reach the redirect address before the complete method is executed.
app.get('/oauth', function(request, response){
Instagram.oauth.ask_for_access_token({
request: request,
response: response,
- redirect: 'http://your.redirect/url', // deprecated
+ redirect: 'http://your.redirect/url', // optional
respond: function(response){
response.redirect('http://your.redirect/url');
// or some other response ended with
@@ -17,7 +17,7 @@ class InstagramAPI
method: "GET"
path: ''
headers: {
- 'User-Agent': 'Instagram Node Lib 0.0.5'
+ 'User-Agent': 'Instagram Node Lib 0.0.6'
'Accept': 'application/json'
'Content-Length': 0
}
@@ -97,9 +97,12 @@ class InstagramAPI
_request: (params) ->
options = @_clone(@_options)
- options['path'] = params['path'] if params['path']?
+ options['host'] = params['host'] if params['host']?
+ options['port'] = params['port'] if params['port']?
options['method'] = params['method'] if params['method']?
+ options['path'] = params['path'] if params['path']?
complete = params['complete'] ?= @_complete
+ appResponse = params['response']
error = params['error'] ?= @_error
if options['method'] isnt "GET" and params['post_data']?
post_data = @_to_querystring params['post_data']
@@ -117,7 +120,7 @@ class InstagramAPI
if parsedResponse? and parsedResponse['meta']? and parsedResponse['meta']['code'] isnt 200
error parsedResponse['meta']['error_type'], parsedResponse['meta']['error_message'], "_request"
else if parsedResponse['access_token']?
- complete parsedResponse
+ complete parsedResponse, appResponse
else
pagination = if typeof parsedResponse['pagination'] is 'undefined' then {} else parsedResponse['pagination']
complete parsedResponse['data'], pagination
@@ -19,7 +19,7 @@
method: "GET",
path: '',
headers: {
- 'User-Agent': 'Instagram Node Lib 0.0.5',
+ 'User-Agent': 'Instagram Node Lib 0.0.6',
'Accept': 'application/json',
'Content-Length': 0
}
@@ -131,15 +131,22 @@
return params;
};
InstagramAPI.prototype._request = function(params) {
- var complete, error, options, post_data, request, _ref, _ref2;
+ var appResponse, complete, error, options, post_data, request, _ref, _ref2;
options = this._clone(this._options);
- if (params['path'] != null) {
- options['path'] = params['path'];
+ if (params['host'] != null) {
+ options['host'] = params['host'];
+ }
+ if (params['port'] != null) {
+ options['port'] = params['port'];
}
if (params['method'] != null) {
options['method'] = params['method'];
}
+ if (params['path'] != null) {
+ options['path'] = params['path'];
+ }
complete = (_ref = params['complete']) != null ? _ref : params['complete'] = this._complete;
+ appResponse = params['response'];
error = (_ref2 = params['error']) != null ? _ref2 : params['error'] = this._error;
if (options['method'] !== "GET" && (params['post_data'] != null)) {
post_data = this._to_querystring(params['post_data']);
@@ -161,7 +168,7 @@
if ((parsedResponse != null) && (parsedResponse['meta'] != null) && parsedResponse['meta']['code'] !== 200) {
return error(parsedResponse['meta']['error_type'], parsedResponse['meta']['error_message'], "_request");
} else if (parsedResponse['access_token'] != null) {
- return complete(parsedResponse);
+ return complete(parsedResponse, appResponse);
} else {
pagination = typeof parsedResponse['pagination'] === 'undefined' ? {} : parsedResponse['pagination'];
return complete(parsedResponse['data'], pagination);
@@ -12,28 +12,27 @@ class InstagramOAuth
return "https://#{@parent._options['host']}/oauth/authorize/?#{@parent._to_querystring(params)}"
ask_for_access_token: (params) ->
- parsedUrl = url.parse(params['request'].url, true)
- console.log parsedUrl
- if parsedUrl['query']['error']?
- @parent._error "#{parsedUrl['query']['error']}: #{parsedUrl['query']['error_reason']}: #{parsedUrl['query']['error_description']}", parsedUrl['query'], 'handshake'
- else if parsedUrl['query']['code']?
+ parsed_query = url.parse(params['request'].url, true).query
+ if parsed_query.error?
+ @parent._error "#{parsed_query.error}: #{parsed_query.error_reason}: #{parsed_query.error_description}", parsed_query, 'handshake'
+ else if parsed_query.code?
+ console.log "SURPRISE"
token_params =
complete: params['complete']
+ response: params['response']
+ host: if process.env['TEST_HOST']? then process.env['TEST_HOST'] else @parent._options['host']
+ port: if process.env['TEST_PORT']? then process.env['TEST_PORT'] else @parent._options['port']
method: "POST"
- path: "/oauth/access_token"
+ path: if process.env['TEST_HOST']? then "/fake/oauth/access_token" else "/oauth/access_token"
post_data:
client_id: @parent._config.client_id
client_secret: @parent._config.client_secret
grant_type: 'authorization_code'
redirect_uri: if params['redirect_uri'] is undefined or params['redirect_uri'] is null then @parent._config.redirect_uri else params['redirect_uri']
- code: parsedUrl['query']['code']
+ code: parsed_query.code
@parent._request token_params
- if params['respond']?
- return params['respond'](params['response'])
- else if params['redirect']?
+ if params['redirect']?
params['response'].redirect(params['redirect']);
- else
- params['response'].writeHead 200, { 'Content-Length': 0, 'Content-Type': 'text/plain' }
- params['response'].end()
+ params['response'].end()
module.exports = InstagramOAuth
@@ -12,36 +12,32 @@
return "https://" + this.parent._options['host'] + "/oauth/authorize/?" + (this.parent._to_querystring(params));
};
InstagramOAuth.prototype.ask_for_access_token = function(params) {
- var parsedUrl, token_params;
- parsedUrl = url.parse(params['request'].url, true);
- console.log(parsedUrl);
- if (parsedUrl['query']['error'] != null) {
- return this.parent._error("" + parsedUrl['query']['error'] + ": " + parsedUrl['query']['error_reason'] + ": " + parsedUrl['query']['error_description'], parsedUrl['query'], 'handshake');
- } else if (parsedUrl['query']['code'] != null) {
+ var parsed_query, token_params;
+ parsed_query = url.parse(params['request'].url, true).query;
+ if (parsed_query.error != null) {
+ return this.parent._error("" + parsed_query.error + ": " + parsed_query.error_reason + ": " + parsed_query.error_description, parsed_query, 'handshake');
+ } else if (parsed_query.code != null) {
+ console.log("SURPRISE");
token_params = {
complete: params['complete'],
+ response: params['response'],
+ host: process.env['TEST_HOST'] != null ? process.env['TEST_HOST'] : this.parent._options['host'],
+ port: process.env['TEST_PORT'] != null ? process.env['TEST_PORT'] : this.parent._options['port'],
method: "POST",
- path: "/oauth/access_token",
+ path: process.env['TEST_HOST'] != null ? "/fake/oauth/access_token" : "/oauth/access_token",
post_data: {
client_id: this.parent._config.client_id,
client_secret: this.parent._config.client_secret,
grant_type: 'authorization_code',
redirect_uri: params['redirect_uri'] === void 0 || params['redirect_uri'] === null ? this.parent._config.redirect_uri : params['redirect_uri'],
- code: parsedUrl['query']['code']
+ code: parsed_query.code
}
};
this.parent._request(token_params);
- if (params['respond'] != null) {
- return params['respond'](params['response']);
- } else if (params['redirect'] != null) {
+ if (params['redirect'] != null) {
params['response'].redirect(params['redirect']);
- } else {
- params['response'].writeHead(200, {
- 'Content-Length': 0,
- 'Content-Type': 'text/plain'
- });
+ return params['response'].end();
}
- return params['response'].end();
}
};
return InstagramOAuth;
View
@@ -34,20 +34,120 @@ app.get PATH,
(request, response) ->
Instagram.subscriptions.handshake request, response
+app.get '/fake/oauth/authorize',
+ (request, response) ->
+ params = url.parse(request.url, true).query
+ response.writeHead 302, {'Location': "#{params.redirect_uri}?code=some-test-code"}
+ response.end()
+
+app.post '/fake/oauth/access_token',
+ (request, response) ->
+ querystring = require 'querystring'
+ util = require 'util'
+ should = require 'should'
+ fake_response =
+ meta:
+ code: 200
+ data:
+ access_token: 'fb2e77d.47a0479900504cb3ab4a1f626d174d2d'
+ user:
+ id: 1574083
+ username: 'snoopdogg'
+ full_name: 'Snoop Dogg'
+ profile_picture: 'http://distillery.s3.amazonaws.com/profiles/profile_1574083_75sq_1295469061.jpg'
+ pagination: {}
+ data = ''
+ request.on 'data', (chunk) ->
+ data += chunk
+ request.on 'end', ->
+ parsed = querystring.parse data
+ parsed.should.have.property 'client_id'
+ parsed.client_id.should.have.property 'length', 32
+ parsed.should.have.property 'client_secret'
+ parsed.client_secret.should.have.property 'length', 32
+ parsed.should.have.property 'grant_type', 'authorization_code'
+ parsed.should.have.property 'redirect_uri', Instagram._config.redirect_uri
+ parsed.should.have.property 'code', 'some-test-code'
+ console.log " access_token request data met assertions at /fake/oauth/access_token"
+ response.writeHead 200, {'Content-Type': 'application/json'}
+ response.end(JSON.stringify(fake_response))
+
app.get '/oauth',
(request, response) ->
+ parsed_query = url.parse(request.url, true).query
+ if parsed_query.error?
+ @parent._error "#{parsed_query.error}: #{parsed_query.error_reason}: #{parsed_query.error_description}", parsed_query, 'handshake'
+ else if parsed_query.code?
+ post_data:
+ client_id: Instagram._config.client_id
+ client_secret: Instagram._config.client_secret
+ grant_type: 'authorization_code'
+ redirect_uri: Instagram._config.redirect_uri
+ code: parsed_query.code
+ options = Instagram._clone(Instagram._options)
+ options['host'] = if process.env['TEST_HOST']? then process.env['TEST_HOST'] else Instagram._options['host']
+ options['port'] = if process.env['TEST_PORT']? then process.env['TEST_PORT'] else Instagram._options['port']
+ options['method'] = "POST"
+ options['path'] = if process.env['TEST_HOST']? then "/fake/oauth/access_token" else "/oauth/access_token"
+ post_data = Instagram._to_querystring post_data
+ options['headers']['Content-Length'] = post_data.length
+ complete = (access, appResponse) ->
+ console.log access
+ console.log appResponse
+ appResponse.writeHead 200, {'Content-Type': 'text/plain'}
+ appResponse.end('Successful End-Of-Chain\n')
+ error = Instagram._error
+ http_client = require 'http'
+ token_request = http_client.request options, (token_response) ->
+ data = ''
+ response.setEncoding 'utf8'
+ response.on 'data', (chunk) ->
+ data += chunk
+ response.on 'end', ->
+ console.log "HERE"
+ try
+ parsedResponse = JSON.parse data
+ if parsedResponse? and parsedResponse['meta']? and parsedResponse['meta']['code'] isnt 200
+ error parsedResponse['meta']['error_type'], parsedResponse['meta']['error_message'], "_request"
+ else if parsedResponse['access_token']?
+ complete parsedResponse, response
+ else
+ pagination = if typeof parsedResponse['pagination'] is 'undefined' then {} else parsedResponse['pagination']
+ complete parsedResponse['data'], pagination
+ catch e
+ error e, data, '_request'
+ if post_data?
+ token_request.write post_data
+ token_request.addListener 'error', (connectionException) ->
+ if connectionException.code isnt 'ENOTCONN'
+ console.log "\n" + connectionException
+ throw connectionException
+ token_request.end()
+
+ ###
Instagram.oauth.ask_for_access_token {
request: request,
response: response,
- respond: (response) ->
- response.redirect('/oauth/success')
- response.end()
+ complete: (access, appResponse) ->
+ console.log access
+ console.log appResponse
+ appResponse.writeHead 200, {'Content-Type': 'text/plain'}
+ appResponse.end('Successful End-Of-Chain\n')
}
-
-app.get '/oauth/success',
- (request, response) ->
- response.writeHead 200, {'Content-Type': 'text/plain'}
- response.end('Successful\n')
+ fake_response =
+ meta:
+ code: 200
+ data:
+ access_token: 'fb2e77d.47a0479900504cb3ab4a1f626d174d2d'
+ user:
+ id: 1574083
+ username: 'snoopdogg'
+ full_name: 'Snoop Dogg'
+ profile_picture: 'http://distillery.s3.amazonaws.com/profiles/profile_1574083_75sq_1295469061.jpg'
+ pagination: {}
+ response.writeHead 200, {'Content-Type': 'application/json'}
+ response.end(JSON.stringify(fake_response))
+ ###
app.listen PORT
Oops, something went wrong.

0 comments on commit bf00d7d

Please sign in to comment.