0

i have a requirement for creating PostgreSQL instance using google SQL admin API, for the authentication, i want to use OAuth2Client in node js client library

    function first() { 
           const oAuth2Client = new google.auth.OAuth2(
                  client_id,
                  client_secret,
                  redirect_uris[0]
                );
            var tkn = await oAuth2Client.getToken(code_from_another_user);
                oAuth2Client.setCredentials(tkn);
    return oAuth2Client;
   });
    function second(oAuth2Client)
    {
            var req = {
                 project: prjName,
                  resource: {
                    databaseVersion: "POSTGRES_13",
                    name: name,
                    region: cregion ,
                    gceZone: czone,
                    settings: {*****}
                    rootPassword: "xxxxx",
                  },
                  auth: oAuth2Client,
                };
                var crpsql = await sqlAdmin.instances.insert(req);
                return crpsql;
    });


function mainexec()
{
 var a = first();
var b = second(a);
});

and i get this error

Error: No access, refresh token, API key or refresh handler callback is set

here actually, i am trying to create a PostgreSQL instance on other google account cloud platform with there consent using OAuth2Client access token method. anyone please help? any supporting documentation?.

1 Answer 1

1

The function first returns oAuth2Client as it is . But in the function second it is converted to JSON object automatically. so changed the function named second like this

function second(oAuth2Client)
    {
var newoAuth2Client = new google.auth.OAuth2(
      oAuth2Client._clientId,
      oAuth2Client._clientSecret,
      oAuth2Client.redirectUri
    );
    var tokenObj = {
      access_token: oAuth2Client.credentials.tokens.access_token,
      refresh_token: oAuth2Client.credentials.tokens.refresh_token,
    };
    newoAuth2Client.credentials = tokenObj;
            var req = {
                 project: prjName,
                  resource: {
                    databaseVersion: "POSTGRES_13",
                    name: name,
                    region: cregion ,
                    gceZone: czone,
                    settings: {*****}
                    rootPassword: "xxxxx",
                  },
                  auth:  newoAuth2Client,
                };
                var crpsql = await sqlAdmin.instances.insert(req);
                return crpsql;
    });

it worked like a magic.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.