无法连接到 Google 驱动器 API
Having issues to connect to Google Drive API
我正在尝试连接到 Google 驱动器 API,但我一直收到 return“未捕获类型错误:无法读取未定义的 属性 'init'”在 initClient.
我不知道如何解决这个问题。
这是我的代码 运行:
`enter code here`var CLIENT_ID = ' ';
var API_KEY = ' ';
// Array of API discovery doc URLs for APIs used by the quickstart
var DISCOVERY_DOCS = ["https://www.googleapis.com/discovery/v1/apis/drive/v3/rest"];
// Authorization scopes required by the API; multiple scopes can be
// included, separated by spaces.
var SCOPES = 'https://www.googleapis.com/auth/drive.readonly';
var FILES = {};
function handleClientLoad() {
setTimeout(function(){ gapi.load('client', initClient()); }, 3000);
}
function initClient() {
gapi.client.init({
apiKey: API_KEY,
clientId: CLIENT_ID,
discoveryDocs: DISCOVERY_DOCS,
scope: SCOPES
}).then(function () {
// Listen for sign-in state changes.
gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);
// Handle the initial sign-in state.
updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
authorizeButton.onclick = handleAuthClick();
signoutButton.onclick = handleSignoutClick();
}, function(error) {
console.log(JSON.stringify(error, null, 2));
});
return gapi;
}
我按照建议进行了更改,但仍然无法正常工作。我不确定我做错了什么!这是更新后的代码:
`enter code here`var CLIENT_ID = ' ';
var API_KEY = ' ';
// Array of API discovery doc URLs for APIs used by the quickstart
var DISCOVERY_DOCS = ["https://www.googleapis.com/discovery/v1/apis/drive/v3/rest"];
// Authorization scopes required by the API; multiple scopes can be
// included, separated by spaces.
var SCOPES = 'https://www.googleapis.com/auth/drive.readonly';
var FILES = {};
function handleClientLoad() {
setTimeout(function(){ gapi.load('client:auth2', initClient); }, 3000);
}
function initClient() {
gapi.client.init({
apiKey: API_KEY,
clientId: CLIENT_ID,
discoveryDocs: DISCOVERY_DOCS,
scope: SCOPES
}).then(function () {
// Listen for sign-in state changes.
gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);
// Handle the initial sign-in state.
updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
authorizeButton.onclick = handleAuthClick();
signoutButton.onclick = handleSignoutClick();
}, function(error) {
console.log(JSON.stringify(error, null, 2));
});
return gapi;
}
还有其他建议吗?谢谢!
gapi.load 接受一个回调函数,但您将它传递给 initClient() 的输出。
其次,您需要加载 OAuth2 库 (auth2)。
改变这个:
function handleClientLoad() {
setTimeout(function() { gapi.load('client', initClient()); }, 3000);
为此:
function handleClientLoad() {
setTimeout(function() { gapi.load('client:auth2', initClient); }, 3000);
PS。如果您将代码放在问题的文本中,而不是屏幕截图,其他人剪切粘贴答案会容易得多。
我正在尝试连接到 Google 驱动器 API,但我一直收到 return“未捕获类型错误:无法读取未定义的 属性 'init'”在 initClient.
我不知道如何解决这个问题。
这是我的代码 运行:
`enter code here`var CLIENT_ID = ' ';
var API_KEY = ' ';
// Array of API discovery doc URLs for APIs used by the quickstart
var DISCOVERY_DOCS = ["https://www.googleapis.com/discovery/v1/apis/drive/v3/rest"];
// Authorization scopes required by the API; multiple scopes can be
// included, separated by spaces.
var SCOPES = 'https://www.googleapis.com/auth/drive.readonly';
var FILES = {};
function handleClientLoad() {
setTimeout(function(){ gapi.load('client', initClient()); }, 3000);
}
function initClient() {
gapi.client.init({
apiKey: API_KEY,
clientId: CLIENT_ID,
discoveryDocs: DISCOVERY_DOCS,
scope: SCOPES
}).then(function () {
// Listen for sign-in state changes.
gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);
// Handle the initial sign-in state.
updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
authorizeButton.onclick = handleAuthClick();
signoutButton.onclick = handleSignoutClick();
}, function(error) {
console.log(JSON.stringify(error, null, 2));
});
return gapi;
}
我按照建议进行了更改,但仍然无法正常工作。我不确定我做错了什么!这是更新后的代码:
`enter code here`var CLIENT_ID = ' ';
var API_KEY = ' ';
// Array of API discovery doc URLs for APIs used by the quickstart
var DISCOVERY_DOCS = ["https://www.googleapis.com/discovery/v1/apis/drive/v3/rest"];
// Authorization scopes required by the API; multiple scopes can be
// included, separated by spaces.
var SCOPES = 'https://www.googleapis.com/auth/drive.readonly';
var FILES = {};
function handleClientLoad() {
setTimeout(function(){ gapi.load('client:auth2', initClient); }, 3000);
}
function initClient() {
gapi.client.init({
apiKey: API_KEY,
clientId: CLIENT_ID,
discoveryDocs: DISCOVERY_DOCS,
scope: SCOPES
}).then(function () {
// Listen for sign-in state changes.
gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);
// Handle the initial sign-in state.
updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
authorizeButton.onclick = handleAuthClick();
signoutButton.onclick = handleSignoutClick();
}, function(error) {
console.log(JSON.stringify(error, null, 2));
});
return gapi;
}
还有其他建议吗?谢谢!
gapi.load 接受一个回调函数,但您将它传递给 initClient() 的输出。
其次,您需要加载 OAuth2 库 (auth2)。
改变这个:
function handleClientLoad() {
setTimeout(function() { gapi.load('client', initClient()); }, 3000);
为此:
function handleClientLoad() {
setTimeout(function() { gapi.load('client:auth2', initClient); }, 3000);
PS。如果您将代码放在问题的文本中,而不是屏幕截图,其他人剪切粘贴答案会容易得多。