- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试按照 Google 的 Node API 访问 gmail here .
当以 node quickstart.js
运行他们的示例快速入门时,出现以下错误:
TypeError: Cannot read property 'client_secret' of undefined
at authorize (/home/user/example/quickstart.js:32:43)
at processClientSecrets (/home/example/bstick/quickstart.js:21:3)
at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:439:3)
client_secret.json
,并将其复制到quickstart.js
的工作目录中node --version
= 7.8.0
, package.json 有 "google-auth-library": "^0.10.0",
“googleapis”:“^19.0.0”
源代码(谷歌的例子):
var fs = require('fs');
var readline = require('readline');
var google = require('googleapis');
var googleAuth = require('google-auth-library');
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/gmail-nodejs-quickstart.json
var SCOPES = ['https://www.googleapis.com/auth/gmail.readonly'];
var TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH ||
process.env.USERPROFILE) + '/.credentials/';
var TOKEN_PATH = TOKEN_DIR + 'gmail-nodejs-quickstart.json';
// Load client secrets from a local file.
fs.readFile('client_secret.json', function processClientSecrets(err, content) {
if (err) {
console.log('Error loading client secret file: ' + err);
return;
}
// Authorize a client with the loaded credentials, then call the
// Gmail API.
authorize(JSON.parse(content), listLabels);
});
/**
* Create an OAuth2 client with the given credentials, and then execute the
* given callback function.
*
* @param {Object} credentials The authorization client credentials.
* @param {function} callback The callback to call with the authorized client.
*/
function authorize(credentials, callback) {
var clientSecret = credentials.installed.client_secret;
var clientId = credentials.installed.client_id;
var redirectUrl = credentials.installed.redirect_uris[0];
var auth = new googleAuth();
var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);
// Check if we have previously stored a token.
fs.readFile(TOKEN_PATH, function(err, token) {
if (err) {
getNewToken(oauth2Client, callback);
} else {
oauth2Client.credentials = JSON.parse(token);
callback(oauth2Client);
}
});
}
/**
* Get and store new token after prompting for user authorization, and then
* execute the given callback with the authorized OAuth2 client.
*
* @param {google.auth.OAuth2} oauth2Client The OAuth2 client to get token for.
* @param {getEventsCallback} callback The callback to call with the authorized
* client.
*/
function getNewToken(oauth2Client, callback) {
var authUrl = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES
});
console.log('Authorize this app by visiting this url: ', authUrl);
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Enter the code from that page here: ', function(code) {
rl.close();
oauth2Client.getToken(code, function(err, token) {
if (err) {
console.log('Error while trying to retrieve access token', err);
return;
}
oauth2Client.credentials = token;
storeToken(token);
callback(oauth2Client);
});
});
}
/**
* Store token to disk be used in later program executions.
*
* @param {Object} token The token to store to disk.
*/
function storeToken(token) {
try {
fs.mkdirSync(TOKEN_DIR);
} catch (err) {
if (err.code != 'EEXIST') {
throw err;
}
}
fs.writeFile(TOKEN_PATH, JSON.stringify(token));
console.log('Token stored to ' + TOKEN_PATH);
}
/**
* Lists the labels in the user's account.
*
* @param {google.auth.OAuth2} auth An authorized OAuth2 client.
*/
function listLabels(auth) {
var gmail = google.gmail('v1');
gmail.users.labels.list({
auth: auth,
userId: 'me',
}, function(err, response) {
if (err) {
console.log('The API returned an error: ' + err);
return;
}
var labels = response.labels;
if (labels.length == 0) {
console.log('No labels found.');
} else {
console.log('Labels:');
for (var i = 0; i < labels.length; i++) {
var label = labels[i];
console.log('- %s', label.name);
}
}
});
}
最佳答案
看来您在生成凭据时一定选择了 OAuth 以外的东西。
所以按照步骤你的配置文件应该是这样的
{"installed": //whatever
但是如果你选择 Web,那么你的配置文件将如下所示
{"web":{ //whatever
因此打开您的配置文件并根据该文件更改此行。
var clientSecret = credentials.installed.client_secret;
//it should be credentials.whatever-key.client_secret.
//Example : If its web then this line should be credentials.web.client_secret.
关于javascript - 类型错误 : Cannot read property 'client_secret' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43639565/
我正在尝试在 here 上可用的 google drive 的命令行演示应用程序.我已按照在 google api 控制台中创建新项目的所有步骤进行操作,然后下载了 client_secret.jso
我使用 ADAL for android 编写了下面的身份验证代码: mAuthContext = new AuthenticationContext(MainActivity.this, Const
对于用户身份验证,我使用最新的@azure/msal-angular 包。这个 Angular 库基于用于 AD 身份验证的最新 MSAL 库。但是库会抛出以下错误: ServerError: inv
我正在尝试回收 AKS 集群中的 service_principal:client_secret 值,该值是由 terraform 使用 azurerm_kubernetes_cluster 上的文档
我有以下代码: - (IBAction)connectToGoogleAnalyticsPressed:(id)sender { GPPSignIn *signIn = [GPPSignIn
我正在使用 PyDrive 将文件从我使用 Pyinstaller 打包的桌面 Python 应用程序上传到 GoogleDrive。我想尽可能地隐藏 client_secrets.json,所以我将
我尝试按照 https://developers.google.com/google-apps/activity/v1/quickstart/nodejs 中的说明使用 Node.js 创建示例程序
有点 OAUTH 的初学者,想问我是否理解正确。我正在使用 OWIN 和 C#,并设置以下场景: 用户向我的 token 端点发出请求,传入用户名/密码和密码 grant_type。如果凭据有效,那么
我正在使用 Python 中的 Google Tasks API 编写程序。我从 Google 下载并运行了示例应用程序,它带有一个名为 client_secrets.json 的文件。 .我知道该文
我目前的任务是弄清楚如何严格从客户端 JavaScript 充分利用 OAuth 2.0 提供程序,并且担心会暴露应用程序的 client_secret。这本质上是 OAuth 客户端的“密码”。 h
为了从 Facebook 获得 access_token,您必须传输您的 app_id、您在授权请求后收到的 code 以及您应用的 secret_key。 为什么我要EVER 传输我的 key ?这
我对这个东西很陌生,我真的不知道现在该怎么办,我需要能够输入“client_secret.json”文件的确切路径,但在每个教程中我看到它只使用中的文件名相同的目录,但我的不是。 creds = Se
我正在尝试从 google API 下载 client_secret.json。我正在执行 https://developers.google.com/gmail/api/quickstart/rub
我正在开发具有自己的 oAuth 授权的 Google chrome 扩展程序。当然,我必须使用 client_id 和 client_secret 作为请求 token 。 有什么办法可以向用户隐藏
我正在构建 Laravel 后端来验证来自移动设备的用户。并使用 https://github.com/lucadegasperi/oauth2-server-laravel 手机用户调用/oauth
阅读此处的一些答案,人们建议通过以下方式调用 Foursquare 进行无用户访问: https://api.foursquare.com/v2/venues/search?ll=40.7,-74&c
有没有什么巧妙的存储方法client_id和 client_secret安全地在客户端应用程序中? 这可能是一个被反编译的 Android/iOS 应用程序。或以纯文本形式提供的 Chrome 扩展程
我有一个可以在我的计算机上使用 dev_appserver.py 运行的应用程序。我部署成功: ronj@l:~$ appcfg.py update myapp/ 03:03 PM Host: app
我正在使用 google admin api 并将示例代码放在一起并稍作修改。我在谷歌端正确设置了东西,但是当我运行脚本时它一直给我错误。下面是我在 python 中的代码: from __futur
我正在尝试按照 Google 的 Node API 访问 gmail here . 当以 node quickstart.js 运行他们的示例快速入门时,出现以下错误: TypeError: Cann
我是一名优秀的程序员,十分优秀!