Patreon / patreon-js

Use the Patreon API via OAuth.
MIT License
151 stars 30 forks source link

error when implementing #20

Closed t5unamie closed 6 years ago

t5unamie commented 6 years ago

using nodejs with express.

Took your example code and created a route as shown below.

var express = require('express');
var router = express.Router();
var url = require('url');
var patreon = require('patreon');
var patreonAPI = patreon
var patreonOAuth = patreon.oauth

// Use the client id and secret you received when setting up your OAuth account
var CLIENT_ID = 'XXX_CLIENT_ID_XXX'
var CLIENT_SECRET = 'XXX_CLIENT_SECRET_XXX'
var patreonOAuthClient = patreonOAuth(CLIENT_ID, CLIENT_SECRET)
// This should be one of the fully qualified redirect_uri you used when setting up your oauth account
var redirectURL = 'https://XXXXXXX/patreonAuth'

const loginUrl = url.format({
    protocol: 'https',
    host: 'patreon.com',
    pathname: '/oauth2/authorize',
    query: {
        response_type: 'code',
        client_id: CLIENT_ID,
        redirect_uri: redirectURL,
        state: 'chill'
    }
})

function handleOAuthRedirectRequest(request, response) {
    var oauthGrantCode = url.parse(request.url, true).query.code

    patreonOAuthClient
        .getTokens(oauthGrantCode, redirectURL)
        .then(function(tokensResponse) {
            console.log(patreonAPI);
            console.log(tokensResponse);
            var patreonAPIClient = patreonAPI(tokensResponse.access_token)
            return patreonAPIClient('/current_user')
        })
        .then(function(result) {

            var store = result.store
            // store is a [JsonApiDataStore](https://github.com/beauby/jsonapi-datastore)
            // You can also ask for result.rawJson if you'd like to work with unparsed data
            response.end(store.findAll('user').map(user => user.serialize()))
        })
        .catch(function(err) {
            console.error('error!', err)
            response.end(err)
        })
}

router.get('/pateronLogin', function(req, res) {
    res.send(`<a href="${loginUrl}">Login with Patreon</a>`)
});

router.get('/patreonAuth', function(req, res) {
   handleOAuthRedirectRequest(req, res)
});

module.exports = router;

I get the following error out after I get redirected and login

error! TypeError: patreonAPI is not a function
    at /opt/node-app/routes/api/auth/patreonv1.js:36:36
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:118:7)
(node:50) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be one of type string or Buffer
    at ServerResponse.end (_http_outgoing.js:747:13)
    at /opt/node-app/routes/api/auth/patreonv1.js:48:22
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:118:7)

when I console.log(patreonAPI);

{ oauth: [Function: oauth],
  patreon: [Function: patreon],
  jsonApiURL: [Function: jsonApiURL] }
t5unamie commented 6 years ago

console.log(tokensResponse);

{ access_token: '<REMOVED_FOR_SECURITY>',
  expires_in: 2678400,
  token_type: 'Bearer',
  scope: 'users pledges-to-me my-campaign',
  refresh_token: '<REMOVED_FOR_SECURITY>',
  version: '0.0.1' }
t5unamie commented 6 years ago

using the following

var patreon = require('patreon');
var patreonAPI = patreon.default
var patreonOAuth = patreon.oauth

shows an output of undefined when

console.log(patreonAPI);
t5unamie commented 6 years ago

Okay when I replace the following

var patreonAPIClient = patreonAPI(tokensResponse.access_token)

with the following

var patreonAPIClient = patreonAPI.patreon(tokensResponse.access_token)

I fix one error.

FYI

Version below

npm -v 5.6.0 node --version v9.8.0 npm list --depth=0 +-- body-parser@1.18.2 +-- cookie-parser@1.4.3 +-- debug@2.2.0 +-- express@4.16.3 +-- jade@1.11.0 +-- js-md5@0.7.3 +-- jsonwebtoken@7.4.3 +-- mongoose@4.13.12 +-- morgan@1.9.0 +-- nodemon@1.17.2 +-- passport-patreon@1.0.1 +-- patreon@0.4.0 +-- randomstring@1.1.5 +-- serve-favicon@2.4.5 `-- url@0.11.0

t5unamie commented 6 years ago

after debugging it's expecting me to send something in the response as the first argument. Most likely a URL. However not sure what it's expecting can you guys help?

t5unamie commented 6 years ago

Wait I think all the code in here should be my code.

.then(function(result) {

            var store = result.store
            console.log(store);
            // store is a [JsonApiDataStore](https://github.com/beauby/jsonapi-datastore)
            // You can also ask for result.rawJson if you'd like to work with unparsed data
            response.end(store.findAll('user').map(user => user.serialize()))

I am pretty sure your expecting me to send something back to you now. As I keep checking it and it waits on your login page. It just sits there before timing out. As I change the response.end just to give and ok 200. However you seem to be waiting for something.

phildini commented 6 years ago

Hey there! Sorry it's taken us so long to get back to you. Are you still having this issue?

t5unamie commented 6 years ago

Yes

On Thu, 19 Apr 2018, 18:53 Philip James, notifications@github.com wrote:

Hey there! Sorry it's taken us so long to get back to you. Are you still having this issue?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Patreon/patreon-js/issues/20#issuecomment-382824969, or mute the thread https://github.com/notifications/unsubscribe-auth/AD8kDkmi5Qwx28tlYYUQiceRBWgfzxnYks5tqM8QgaJpZM4Su5g4 .

emosesPatreon commented 6 years ago

Hi @t5unamie,

I apologize for the delay again, let's see if we can get your example working.

I'm afraid the example in the README is out of date, and we'll fix it soon. In the meantime, you can look at examples/server.js for a working example. If you're not using es2015, the imports should look like this:

const { patreon, oauth } = require('patreon');
//...
const oauthClient = oauth(CLIENT_ID, CLIENT_SECRET);

Then you can build your oauth function as in your example or the example in server.js, and create the patreonAPI client from the retrieved accessToken:

const patreonAPI = patreon(accessToken)

Was that helpful?

t5unamie commented 6 years ago

Testing now. Will get back to you in the next 2 weeks.

On Sat, 5 May 2018, 00:41 Evan Moses, notifications@github.com wrote:

Hi @t5unamie https://github.com/t5unamie,

I apologize for the delay again, let's see if we can get your example working.

I'm afraid the example in the README is out of date, and we'll fix it soon. In the meantime, you can look at examples/server.js https://github.com/Patreon/patreon-js/blob/master/examples/server.js for a working example. If you're not using es2015, the imports should look like this:

const { patreon, oauth } = require('patreon'); //... const oauthClient = oauth(CLIENT_ID, CLIENT_SECRET);

Then you can build your oauth function as in your example or the example in server.js https://github.com/Patreon/patreon-js/blob/master/examples/server.js#L39, and create the patreonAPI client from the retrieved accessToken:

const patreonAPI = patreon(accessToken)

Was that helpful?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Patreon/patreon-js/issues/20#issuecomment-386761038, or mute the thread https://github.com/notifications/unsubscribe-auth/AD8kDjyGXS2sROiFr1XQxD4wcSkjjypqks5tvOcogaJpZM4Su5g4 .

emosesPatreon commented 6 years ago

Closing for now. Please re-open if you're still having difficulty.

jameslanman commented 4 years ago

Was this ever resolved? I am still getting an error.

hanserino commented 1 year ago

Any updates on this? I'm having the same errors.