Unable to get access_code with Oauth 2.0
When I use Oauth 2.0 authentication, I can get the 'code' through the Authorization URL, but when I fill in the Access Token URL, I can't assign the 'code' that I got earlier However, when I fill in the Access Token URL, I can't assign the 'code' that I got before to the 'access_token', so I can't get the 'access_token', because the 'code' changes all the time, so I need to assign it to the 'access_token', how can I do that?
This is the access token url format I used, where code should be what you got in the previous step.
https://gw.open.1688.com/openapi/http/1/system.oauth2/getToken/YOUR_APPKEY?grant_type=authorization_code&need_refresh_token=true&client_id= YOUR_APPKEY&client_secret= YOUR_APPSECRET&redirect_uri=YOUR_REDIRECT_URI&code=CODE
Best Answer
-
Perhaps this would help you:
To obtain the access token using OAuth 2.0, you need to make a POST request to the Access Token URL with the appropriate parameters.Step-by-Step Guide
- Get the Authorization Code:
- Direct the user to the Authorization URL.
- The user will log in and authorize your app, after which they will be redirected to your redirect URI with a
code
parameter in the query string.
- Exchange Authorization Code for Access Token:
- Use the authorization code obtained in the previous step to request the access token.
- The
code
parameter in your Access Token URL should be dynamically replaced with the authorization code.
Example using JavaScript (Client-side)
Here’s how you can dynamically replace the
code
and make the POST request to obtain the access token.// Function to get the access token
async function getAccessToken(authCode) {
const clientId = 'YOUR_APPKEY';
const clientSecret = 'YOUR_APPSECRET';
const redirectUri = 'YOUR_REDIRECT_URI';
const tokenUrl = `https://gw.open.1688.com/openapi/http/1/system.oauth2/getToken/${clientId}`;
const params = new URLSearchParams();
params.append('grant_type', 'authorization_code');
params.append('need_refresh_token', 'true');
params.append('client_id', clientId);
params.append('client_secret', clientSecret);
params.append('redirect_uri', redirectUri);
params.append('code', authCode);
try {
const response = await fetch(tokenUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: params.toString()
});
if (!response.ok) {
throw new Error('Failed to fetch access token');
}
const data = await response.json();
console.log('Access Token:', data.access_token);
return data.access_token;
} catch (error) {
console.error('Error:', error);
}
}
// Example usage:
// You need to get the 'code' parameter from the query string or however it's passed to your application
const urlParams = new URLSearchParams(window.location.search);
const authCode = urlParams.get('code');
if (authCode) {
getAccessToken(authCode);
} else {
console.error('Authorization code not found in the URL');
}** Was this post helpful? Click Agree or Like below. **
** Did this solve your problem? Accept it as a solution! **0 - Get the Authorization Code:
Answers
-
Perhaps this would help you:
To obtain the access token using OAuth 2.0, you need to make a POST request to the Access Token URL with the appropriate parameters.Step-by-Step Guide
- Get the Authorization Code:
- Direct the user to the Authorization URL.
- The user will log in and authorize your app, after which they will be redirected to your redirect URI with a
code
parameter in the query string.
- Exchange Authorization Code for Access Token:
- Use the authorization code obtained in the previous step to request the access token.
- The
code
parameter in your Access Token URL should be dynamically replaced with the authorization code.
Example using JavaScript (Client-side)
Here’s how you can dynamically replace the
code
and make the POST request to obtain the access token.// Function to get the access token
async function getAccessToken(authCode) {
const clientId = 'YOUR_APPKEY';
const clientSecret = 'YOUR_APPSECRET';
const redirectUri = 'YOUR_REDIRECT_URI';
const tokenUrl = `https://gw.open.1688.com/openapi/http/1/system.oauth2/getToken/${clientId}`;
const params = new URLSearchParams();
params.append('grant_type', 'authorization_code');
params.append('need_refresh_token', 'true');
params.append('client_id', clientId);
params.append('client_secret', clientSecret);
params.append('redirect_uri', redirectUri);
params.append('code', authCode);
try {
const response = await fetch(tokenUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: params.toString()
});
if (!response.ok) {
throw new Error('Failed to fetch access token');
}
const data = await response.json();
console.log('Access Token:', data.access_token);
return data.access_token;
} catch (error) {
console.error('Error:', error);
}
}
// Example usage:
// You need to get the 'code' parameter from the query string or however it's passed to your application
const urlParams = new URLSearchParams(window.location.search);
const authCode = urlParams.get('code');
if (authCode) {
getAccessToken(authCode);
} else {
console.error('Authorization code not found in the URL');
}** Was this post helpful? Click Agree or Like below. **
** Did this solve your problem? Accept it as a solution! **0 - Get the Authorization Code:
Categories
- All Categories
- 1.8K Product Ideas
- 1.8K Ideas Exchange
- 1.5K Connect
- 1.2K Connectors
- 299 Workbench
- 6 Cloud Amplifier
- 8 Federated
- 2.9K Transform
- 100 SQL DataFlows
- 616 Datasets
- 2.2K Magic ETL
- 3.8K Visualize
- 2.5K Charting
- 730 Beast Mode
- 55 App Studio
- 40 Variables
- 682 Automate
- 175 Apps
- 451 APIs & Domo Developer
- 46 Workflows
- 10 DomoAI
- 34 Predict
- 14 Jupyter Workspaces
- 20 R & Python Tiles
- 394 Distribute
- 113 Domo Everywhere
- 275 Scheduled Reports
- 6 Software Integrations
- 121 Manage
- 118 Governance & Security
- 8 Domo Community Gallery
- 38 Product Releases
- 10 Domo University
- 5.4K Community Forums
- 40 Getting Started
- 30 Community Member Introductions
- 108 Community Announcements
- 4.8K Archive