4

I am going to get my subscription list with the help of the YouTube api. I wrote this piece of code.

const { google } = require('googleapis');


const oauth2 = google.oauth2('v2');
const express = require('express')
const app = express()
const port = 3000


const oauth2Client = new google.auth.OAuth2(
    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "xxxxxxxxxxxxxxxxxxxxxxx",
    "http://localhost:3000/auth/google/callback"
);

google.options({
    auth: oauth2Client
});

// generate a url that asks permissions for Blogger and Google Calendar scopes
const scopes = [
  'https://www.googleapis.com/auth/youtube',
  'https://www.googleapis.com/auth/youtube.channel-memberships.creator'
];

const url = oauth2Client.generateAuthUrl({
  // 'online' (default) or 'offline' (gets refresh_token)
  access_type: 'offline',

  // If you only need one scope you can pass it as a string
  scope: scopes
});

app.get('/',(req,res,next)=>{
    res.send(url);
})
let tok = "";
app.get('/auth/google/callback',(req,res,next)=>{
    res.send('Susses Authrations');
    console.log("Code authrations : "+req.query.code);
     const {tokens} = oauth2Client.getToken(req.query.code)
     oauth2Client.setCredentials(tokens);
     oauth2Client.on('tokens', (tokens) => {
        if (tokens.refresh_token) {
          // store the refresh_token in my database!
          console.log("refresh_token : "+ tokens.refresh_token);
          tok =  tokens.access_token;
        }
        console.log("Assess Token : "+ tokens.access_token);
      });
})

app.get('/youtube',(req,res,next)=>{
    const youtube = google.youtube('v3',{
        'access_token':oauth2Client.credentials.access_token,
        'refresh_token':oauth2Client.credentials.refresh_token,
        'api_key':oauth2Client.credentials.api_key
    });

    youtube.channels.list({
        "part": [
          "snippet,contentDetails,statistics"
        ],
        "id": [
          "UC_x5XG1OV2P6uZZ5FSM9Ttw"
        ]
      }).then(e=>{
        console.log(e.request)
    })
   

})


app.listen(port,()=>{
    console.log("Hello World")
});

But unfortunately I encounter an error (Error: No access, refresh token or API key is set.) Which apparently does not recognize my refresh token. I am a novice and thank you for guiding me. I also use the clinet id and clinet secret I also built a console inside Google and activated YouTube related libraries.

Afshin
  • 41
  • 1

0 Answers0