fakeshadow / pxs-psn-api

a psn api wrapper
29 stars 10 forks source link

Mandatory parameter \'code\' is missing' #1

Closed OliviaB14 closed 5 years ago

OliviaB14 commented 5 years ago

Hi I'm having problems with the access token i think. Here's the error and the code I use Do you have any idea why it doesn't work?

Error message { error: 'invalid_request', error_description: 'Mandatory parameter \'code\' is missing', docs: 'https://auth.api.sonyentertainmentnetwork.com/docs/', error_code: 4098 }

Code I used

const uuid = '********'
const tfa = '*******'

// return a promise contain access_token and refresh_token.
const { access_token, refresh_token } = api.getAcceeToken(uuid, tfa)

// access_token is used to call other APIs and refresh_token is used to get new access_token when it's expired.
const tokenNew = api.refreshAccessToken(refresh_token)

// use the accessToken to get user profile
const profile = api.getProfile('olibeeear', access_token)

api.getAcceeToken(uuid, tfa).then(function (res1, res2) {
  console.log(res2, res1)
})
fakeshadow commented 5 years ago

Hi. Did you try to wrap the code in an async await?

async function main () {
const { access_token, refresh_token } = await api.getAcceeToken(uuid, tfa);
console.log(access_token, refresh_token);
}
main();
OliviaB14 commented 5 years ago

It returns an invalid access token error { error: { code: 2105857, message: 'Invalid access token' } } undefined undefined

fakeshadow commented 5 years ago

Ok. Maybe try get a new pair of uuid and 2fa code. There are 3 three steps to get the access token and your uuid and 2fa are consumed and become expired after the first step. You can use the individual api call the monitor all three steps and check what step goes wrong like below.

You have to change the source code and export these three functions BTW.

async function main () {
    try {
        const { npsso } = await getNpsso(uuid, tfa);
        console.log(npsso);  

        const grantcode = await getGrant(npsso);
        console.log(grantcode);

        const { access_token, refresh_token } = getToken(grantcode);
        console.log(access_token, refresh_token);
    } catch(e) {
        console.log(e);
    }
}
main();

If you required npsso and/or grantcode successfully. They can be used to call the next step instead of requring a new pair of uuid and 2fa code

OliviaB14 commented 5 years ago

Your code returns me undefined null undefined undefined

I did get a new pair of uuid and tfa with this link though : Bungie, so I don't know what I'm doing wrong?

fakeshadow commented 5 years ago

Your code returns me undefined null undefined undefined

I did get a new pair of uuid and tfa with this link though : Bungie, so I don't know what I'm doing wrong?

Hello I just update the code to use request again and made a more clear readme. Can you try the example again with a new pair of code please? It seems the problem is from node fetch which can't handle redirect page correctly. It used to work fine though.

OliviaB14 commented 5 years ago

YES! It finally works thank you :)

One more question : When I retry, the console says error: invalid_ticket. How can automatically get a new token ?

fakeshadow commented 5 years ago

YES! It finally works thank you :)

One more question : When I retry, the console says error: invalid_ticket. How can automatically get a new token ?

Like I said in the previous reply. Your uuid and 2fa code are consumed in the getting token process therefore they can't be use again. I suggest store your refresh token and access token in a getter setter like in the example. Or use fs.write to store them in a file, you could also store them in a database

The access token can barely last about an hour before it expires. and you can use the refresh token to get a new one whenever you like to.