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

Tagged:

Best Answer

  • ArborRose
    ArborRose Coach
    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

    1. 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.
    2. 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! **

Answers

  • ArborRose
    ArborRose Coach
    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

    1. 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.
    2. 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! **