简体   繁体   English

如何在Node.js中发现Google OAuth2中的API

[英]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.

相关问题 如何在Node.js控制台中发现对象API? - How to discover Object APIs in the Node.js console? 有人可以使用 ELI5 如何使用 node.js 正确执行离线 Google oauth2 吗? - Can someone ELI5 how to properly do offline Google oauth2 with node.js? Node.js中的Google API - Google APIs in Node.js 从node.js oauth2服务器到服务器客户端(谷歌跟踪身份验证) - node.js oauth2 server to server client (google tracks authentication) 是否有任何Node.js客户端库可以对Twitter,Facebook,Google,LinkedIn等进行OAuth和OAuth2 API调用? - Is there any Node.js client library to make OAuth and OAuth2 API calls to Twitter, Facebook, Google, LinkedIn, etc.? 从Node.js / Javascript连接到OAuth2 API时出现问题? - Issues connecting to OAuth2 API from Node.js/Javascript? 令牌错误:错误请求; 谷歌 OAuth2; Node.js 上的 Passport.js; 能够使用 console.log 数据,但是会出现错误 - TokenError: Bad Request; Google OAuth2; Passport.js on Node.js; Able to console.log data, however delivers error 如何将Google API用作特定的用户服务器端? Node.js - How do I use Google APIs as a specific user server-side? Node.js 为什么不将OAuth2访问令牌存储为HttpOnly安全cookie? 如何在Node.js应用程序中工作? - Why aren't OAuth2 access tokens stored as HttpOnly secure cookies? How would that work in a Node.js application? 保护Node.JS API - Securing Node.JS APIs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM