allegro / allegro-api

Issue tracker and wiki for Allegro REST API
https://developer.allegro.pl/
217 stars 39 forks source link

I have obtained the authorization code, but when trying to obtain the token, it returns a 500 (Internal Server Error). #7529

Closed yangsheng8 closed 1 year ago

yangsheng8 commented 1 year ago

The technology stack used is: nodejs koa。 Sensitive information has been blurred out.

The code is as follows: ` const Koa = require('koa') const Router = require('koa-router') const cors = require('@koa/cors') const axios = require('axios') const base64 = require('base-64') const bodyParser = require('koa-bodyparser')

const app = new Koa() app.use(bodyParser()) const router = new Router() // Use CORS middleware app.use( cors({ origin: 'http://48.119.94.110:88', // Set allowed request origins. }) )

router.post('/getToken', async (ctx) => { const { authorizationCode } = ctx.request.body

// Call the function to retrieve the access token const data = await getAccessToken(authorizationCode)

// Return the access token ctx.body = { data } })

// Handle the retrieval of access token and refresh token router.get('/callback', async (ctx) => { let code = ctx.query.code

// Call the function to retrieve the access token. const data = await getAccessToken(code)

// Return the access token. ctx.body = { data } })

// Retrieve the access token. const getAccessToken = async (authorizationCode) => { const clientId = 'b31410fa4b9qxxx3b24eef555e' const clientSecret = 'VRWJX5UncOimOOzsjBJDsFjIgxxxxxxxEt6g7USOZsnOx7Ciw' const authorization = Buffer.from(${clientId}:${clientSecret}).toString( 'base64' ) const redirectUri = 'http://48.119.94.110:3000/callback' // Specify your redirect URI const tokenUrl = 'https://allegro.pl/auth/oauth/token'

const headers = { Authorization: Basic ${authorization}, 'Content-Type': 'application/x-www-form-urlencoded', }

const content = grant_type=authorization_code&code=${encodeURIComponent( authorizationCode )}&redirect_uri=${redirectUri}

const response = await axios.post(tokenUrl, content, { headers })

return response.data }`

Lukasz-Zurek commented 1 year ago

Could you send me your request in json format? Another thing - I don't see that kind of clientId or redirect-uri in our database.

Lukasz-Zurek commented 1 year ago

After receiving authorization_code - you should call then e.g.:

curl -X POST \
  'https://allegro.pl/auth/oauth/token?grant_type=authorization_code&code=pOPEy9Tq94aEss540azzC7xL6nCJDWto&redirect_uri=http://exemplary.redirect.uri'
  -H 'Authorization: Basic YTI...Hg=' 

Please look at our guide.

yangsheng8 commented 1 year ago

Yes, after receiving the authorization_code, I used the Koa framework in Node.js to replace curl and send a POST request. However, I encountered an error and received a 500 error message. Do you have an example of obtaining a token using Node.js?

Lukasz-Zurek commented 1 year ago

No, we do not have an example of obtaining a token using Node.js. But maybe someone from this forum hopefully can help you in this case.

yangsheng8 commented 1 year ago

No, we do not have an example of obtaining a token using Node.js. But maybe someone from this forum hopefully can help you in this case.

Hello, I am currently using official PHP language to obtain a token. However, the client_id and client_secret in the example are fixed and unchangeable. Our company's requirement is to manage multiple Allegro accounts on our own platform, so the client_id and client_secret are passed in from the front-end as parameters instead of constants.

I tried passing the client_id and client_secret through the URL to main.php using the following code:

define('CLIENT_ID', $_GET['client_id']); 
define('CLIENT_SECRET', $_GET['client_secret']); 

This allowed me to obtain an authorization code. However, when trying to obtain an access token by redirecting to another URL, the client_id and client_secret were lost, resulting in the failure to obtain a token.Could you please advise on any solutions to this issue? Thank you.

Lukasz-Zurek commented 1 year ago

Please check our sample PHP code (with or without PKCE) in our guide, at the end of this particular tutorial.

stale[bot] commented 1 year ago

W tym wątku nie pojawiła się żadna nowa odpowiedź w ciągu 7 dni, dlatego automatycznie oznaczamy go jako przeterminowany. Jeśli w ciągu kolejnych 7 dni nie pojawi się żadna odpowiedź, wątek zostanie zamknięty. Dziękujemy za zaangażowanie w dyskusję i zachęcamy Cię do wypełnienia ankiety dotyczącej naszego wsparcia na forum.


There was no new reply in this thread within 7 days, therefore, we automatically marked it as expired. If no response is received within the next 7 days, this thread will be closed. Thank you for engaging in the discussion. We encourage you to complete our forum support survey.