[英]Cannot read property 'authorize' of undefined react-native-app-auth
[英]Cannot read property 'authorize' of undefined in Google Oauth?
我使用Oauth
开发chrome扩展程序,用户可以通过Google帐户登录。我的代码jquery如下所示。
我还在html文件中包含了js库:
<script src="js/jquery-2.1.1.min.js" type="text/javascript"></script>
<script src="js/client.js"></script>
并具有JS功能:
function handleAuthClick(event) {
gapi.auth.authorize({
client_id: clientID,
scope: scopes,
response_type: 'code token id_token gsession',
access_type: accessType,
immediate: false
}, handleAuthResult);
return false;
}
我的问题是:我无法通过谷歌帐户登录并显示错误: Uncaught TypeError: Cannot read property 'authorize' of undefined
。
注意 :如果我以html身份运行,我可以正常登录,但在用作Chrome扩展程序时我无法登录。
似乎gapi.auth仍然没有初始化,原因是你在进行这个调用之前没有按照正确的步骤来设置它。 所以我只是在这里为您提供一个示例,以便在成功初始化gapi对象时查看是否缺少任何步骤。 在这里
<script type="text/javascript">
(function( gapi ) {
gapi.client.setApiKey(apiKey); // your variable for apiKey
window.setTimeout(checkAuth,1);
function checkAuth() {
gapi.auth.authorize({client_id: clientID, scope: scopes, immediate: true}, handleAuthResult);
}
function handleAuthResult(authResult) {
var authorizeButton = document.getElementById('id-of-your-login-button');
if (authResult && !authResult.error) {
authorizeButton.style.visibility = 'hidden';
makeApiCall();
} else {
authorizeButton.style.visibility = '';
authorizeButton.onclick = handleAuthClick;
}
}
function handleAuthClick(event) {
gapi.auth.authorize({
client_id: clientID,
scope: scopes,
response_type: 'code token id_token gsession',
access_type: accessType,
immediate: false
}, handleAuthResult);
return false;
}
})( gapi );
</script>
您可以安全地将此脚本标记放在按钮html代码下方的body标记中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.