[英]How to discover APIs in Google OAuth2 in Node.js
I've got a problem with .discover method in Node.js. 我在Node.js中遇到.discover方法的问题。 I've got such a piece of code: 我有这样一段代码:
googleapis.discover('oauth2', 'v2').execute(function(err, client){
if(!err)
callback(client);
});
And it throws error: TypeError: Object # has no method 'discover' But in all tutorials there is such a method mentioned. 它抛出错误:TypeError:Object#没有方法'discover'但是在所有教程中都提到了这样的方法。 Does anyone know what's wrong? 有谁知道什么是错的?
I got the same error. 我得到了同样的错误。 I think they updated the googleapis client for Node. 我认为他们为Node更新了googleapis客户端。
Try follow the new syntax or use older version: 请尝试按照新语法或使用旧版本:
var google = require('googleapis');
var OAuth2 = google.auth.OAuth2;
var oauth2Client = new OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL);
var drive = google.drive({ version: 'v2', auth: oauth2Client });
I got the same error. 我得到了同样的错误。 But after looking around for a few hours. 但环顾了几个小时后。 I came across this question and realized they might have changed the API and haven't updated the official documentation. 我遇到了这个问题并意识到他们可能已经更改了API并且没有更新官方文档。
I was also trying this piece of code from this link ( https://developers.google.com/drive/web/quickstart/quickstart-nodejs ) 我也在尝试使用此链接中的这段代码( https://developers.google.com/drive/web/quickstart/quickstart-nodejs )
var googleapis = require('googleapis'), readline = require('readline'); var CLIENT_ID = 'YOUR CLIENT ID HERE', CLIENT_SECRET = 'YOUR CLIENT SECRET HERE', REDIRECT_URL = 'YOUR REDIRECT URL HERE', SCOPE = 'https://www.googleapis.com/auth/drive.file'; var rl = readline.createInterface({ input: process.stdin, output: process.stdout }); var auth = new googleapis.OAuth2Client(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL); googleapis.discover('drive', 'v2').execute(function(err, client) { var url = auth.generateAuthUrl({ scope: SCOPE }); var getAccessToken = function(code) { auth.getToken(code, function(err, tokens) { if (err) { console.log('Error while trying to retrieve access token', err); return; } auth.credentials = tokens; upload(); }); }; var upload = function() { client.drive.files .insert({ title: 'My Document', mimeType: 'text/plain' }) .withMedia('text/plain', 'Hello World!') .withAuthClient(auth).execute(console.log); }; console.log('Visit the url: ', url); rl.question('Enter the code here:', getAccessToken); });
After trying for hours and hours. 经过几个小时的努力。 I changed my quickstart.js to this after looking at some other docs/tutorials on the internet. 在查看了互联网上的其他文档/教程之后,我将quickstart.js改为了。
var googleapis = require('googleapis'); var OAuth2 = googleapis.auth.OAuth2; var readline = require('readline'); var CLIENT_ID = 'YOUR CLIENT ID HERE', CLIENT_SECRET = 'YOUR CLIENT SECRET HERE', REDIRECT_URL = 'YOUR REDIRECT URL HERE', SCOPE = 'https://www.googleapis.com/auth/drive.file'; var rl = readline.createInterface({ input: process.stdin, output: process.stdout }); var oauth2Client = new OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL); var drive = googleapis.drive({ version: 'v2', auth: oauth2Client }); var execute = function(err, client) { var url = oauth2Client.generateAuthUrl({ scope: SCOPE }); var getAccessToken = function(code) { oauth2Client.getToken(code, function(err, tokens) { if (err) { console.log('Error while trying to retrieve access token', err); return; } oauth2Client.credentials = tokens; upload(); }); }; var upload = function() { console.log(client) client.drive.files .insert({ title: 'My Document', mimeType: 'text/plain' }) .withMedia('text/plain', 'Hello World!') .withAuthClient(oauth2Client).execute(console.log); }; console.log('Visit the url: ', url); rl.question('Enter the code here:', getAccessToken); }; execute();
It started working but although I got it to output the URL to go to, I get an error after pasting the authorization code from the URL. 它开始工作但是虽然我得到了输出的URL,但是在从URL粘贴授权代码后我收到错误。 The Client object in the upload function is undefined so I get an type error saying "cannot read property 'drive' of undefined". 上传函数中的Client对象是未定义的,所以我得到一个类型错误,说“无法读取未定义的属性'驱动'”。 If someone knows the problem, please do let me know. 如果有人知道这个问题,请告诉我。 I am also just learning NodeJS. 我也正在学习NodeJS。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.