我正在我的index.html中初始化gapi.auth2
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="google-signin-client_id" content= "817677528939-dss5sreclldv1inb26tb3tueac98d24r.apps.googleusercontent.com">
<meta name="google-signin-scope" content="profile email">
<script src="https://apis.google.com/js/client:platform.js?onload=start" async defer></script>
<script>
function start() {
console.log('script running')
gapi.load('auth2', function() {
auth2 = gapi.auth2.init({
client_id: '817677528939-dss5sreclldv1inb26tb3tueac98d24r.apps.googleusercontent.com',
scope: 'profile email'
});
});
}
</script>
<!-- <meta name="theme-color" content="#000000"> -->
<!-- <link rel="manifest" href="%PUBLIC_URL%/manifest.json"> -->
<!-- <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico"> -->
<title>React App</title>
</head>
<body>
<div id="root"></div>
</body>
</html>
然后在我的反应中:
在页面顶部:/* global gapi */
handleSignIn() {
console.log(gapi.auth2)
gapi.auth2.grantOfflineAccess()
}
console.log
正在打印:
{init: ƒ, authorize: ƒ, _gt: ƒ, enableDebugLogs: ƒ, getAuthInstance: ƒ, …}
所以看起来gapi.auth2
是一个函数,但我收到了错误:
gapi.auth2.grantOfflineAccess is not a function
我做错了什么?
答案 0 :(得分:1)
看起来您没有在grantOfflineAccess
对象上调用GoogleAuth
。要获取GoogleAuth
对象,您需要像这样调用getAuthInstance()
:
var ga = gapi.auth2.getAuthInstance();
ga.grantOfflineAccess();
请务必在首次按指定here致电getAuthInstance()
后致电gapi.auth2.init()
。
答案 1 :(得分:0)
错过了一步:
查看打印的函数我需要先调用另一个函数:
gapi.auth2.getAuthInstance().grantOfflineAccess()