h2non / youtube-video-api

Simplified programmatic and command-line interface for YouTube Video API. Built for node.js
MIT License
67 stars 16 forks source link

Cannot retrieve token #23

Open nikswap opened 8 years ago

nikswap commented 8 years ago

When using the feature where it will get a new token each time do I get the following error:

Error: Cannot retrieve the token. Timeout exceeded
    at Timeout._onTimeout (/home/lladmin/ytnode/node_modules/nightmare-google-oauth2/index.js:179:18)
    at tryOnTimeout (timers.js:224:11)
    at Timer.listOnTimeout (timers.js:198:5)

My oauth config is a s follows: (fetch from client_secret.json)

{
"web": {
        "client_id":"<clientid>",
        "project_id":"our-axon-133011","auth_uri":"https://accounts.google.com/o/oauth2/auth",
        "token_uri":"https://accounts.google.com/o/oauth2/token",
        "auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs",
        "client_secret":"<client_secret>",
        "redirect_uris":["http://localhost:8488"],
        "javascript_origins":["http://localhost"]
    }
}

My code:

var youtube = Youtube({
  video: {
    part: 'status,snippet'
  },
  email: '<name>@gmail.com',
  password: '<password>'
});

youtube.authenticate('<clientid>','<clientsecret>', function (err) {
                if (!err) {
                        youtube.upload('video.webm', {}, function (err, video) {
                                if (!err) {
                                        console.log('Video was uploaded:', video.id);
                                } else {
                                        console.log(err);
                                }
                        });
                } else {
                        console.log(err);
                }
        }
);
h2non commented 8 years ago

Seems like Google changed the login form, so scrapping process is not working fine. Have to review it in detail.

duclvz commented 8 years ago

Please review it!

h2non commented 8 years ago

Will take a look when I'm free. This kind of web scraping automation is very annoying to support since Google is trying to do it as hard as possible.

h2non commented 8 years ago

My suggestion would be to save the OAuth2 credentials from JSON file and reuse it across projects to avoid automatic web token retrieval each time.

duclvz commented 8 years ago

Nice suggestion, it work fine. But could you confirm it will auto regenerate access token before it expires? Thanks!

h2non commented 8 years ago

Yes, I'll try to work on this later this week, however, the generated JSON should include a renewal token, so asking for a fresh token is not be required.

ghost commented 7 years ago

@duclvz Could you provide a sample of your working code? Thanks

coolmonx commented 7 years ago

The auto regenerate access token of mine didn't work, so I wrote a simple code to renew the token myself, and wrote it back to the credential file.

You can check the expires_in value of the access token from the API:

https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=[Your Access Token]

So if it gets less than x secs. or already expired, just renew it.

var oauth2file = './.google-oauth2-credentials.json'; var file = require(oauth2file);

if (expires < 50 || typeof expires == 'undefined') { request({ url: 'https://accounts.google.com/o/oauth2/token', method: 'POST', form: { 'grant_type': 'refresh_token', 'refresh_token': '[Your Refresh Token]', 'client_id': '[Your Client ID]', 'client_secret': '[Your Client Secret]' } }, function(err, res) { if (err) { console.log(err); } var json = JSON.parse(res.body); oauth2file.access_token = json.access_token;

                    fs.writeFile(file, JSON.stringify(oauth2file), function (err) {
                            if (err) return console.log(err);
                    });
        });
    } 

}

Farkal commented 7 years ago

Any news on this ? I am trying to authenticate with :

{"web": {
  "client_id":"my-client-id",
  "project_id":"my-project-id",
  "auth_uri":"https://accounts.google.com/o/oauth2/auth",
  "token_uri":"https://accounts.google.com/o/oauth2/token",
  "auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs",
  "client_secret":"my-client-secret"}
}

But I can't find a way, I still get an error message.

falconscript commented 7 years ago

Pull request should fix this: https://github.com/h2non/youtube-video-api/pull/28

CHARITH1995 commented 5 years ago

Error: Cannot retrieve the token. Timeout exceeded at Timeout._onTimeout (/home/lladmin/ytnode/node_modules/nightmare-google-oauth2/index.js:179:18)

i also getting this error ..what should i do ???

ThibaultJanBeyer commented 4 years ago

It's broken again as it seems… :(

h2non commented 4 years ago

The problem relies on this package: https://github.com/h2non/nightmare-google-oauth2

The implementation uses a sort of weak scraping mechanism, which is easy to break if Google changes their web markup implementation details.

I'm no longer supporting that package and I encourage you to use a more API driven OAuth2 token retrieval instead of web scraping.

ThibaultJanBeyer commented 4 years ago

Do you have an example of such API Driven OAuth2 token retrieval?

What I did for now is create the token once manually and then renew it all the time with a cron job so that I don't have to do it ever again. Ideally, even the first manual step would be unnecessary.

If you stopped supporting that "auto-login" functionality how about removing it from the library to avoid confusion since the remaining parts (once you have a token to use) are great :)

h2non commented 4 years ago

I have no time to work on that, but you can easily bypass the built-in authentication mechanism by overwriting this property with an already authorized OAuth2 instance: https://github.com/h2non/youtube-video-api/blob/master/index.js#L105