我在"js.do"而且没有任何问题。然而,当我在本地主机上运行它时,我得到了以下错误:"Uncaught TypeError: Cannot read property 'signIn' of null"
<script src="https://apis.google.com/js/api.js"></script>
<script>
var CLIENT_ID = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx';
var API_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx';
/**
* Sample JavaScript code for reseller.subscriptions.list
* See instructions for running APIs Explorer code samples locally:
* https://developers.google.com/explorer-help/guides/code_samples#javascript
*/
function authenticate() {
return gapi.auth2.getAuthInstance()
.signIn({scope: "https://www.googleapis.com/auth/apps.order https://www.googleapis.com/auth/apps.order.readonly"})
.then(function() { console.log("Sign-in successful"); },
function(err) { console.error("Error signing in", err); });
}
function loadClient() {code to load client }
// Make sure the client is loaded and sign-in is complete before calling this method.
function execute() {// code to execute API read commands }
function checkStatus (response,counter)
{
console.log("inside checkStatus",response.seats);
return response.seats.numberOfSeats > 0;
}
gapi.load("client:auth2", function() {
gapi.auth2.init({
client_id: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
});
});
</script>
<button onclick="authenticate().then(loadClient)">authorize and load</button>
<button onclick="execute()">execute</button>
这可能是什么原因呢?
gapi.load("client:auth2", function() {...})
是引起问题的原因。我得到这个错误"gapi.auth2。ExternallyVisibleError: Invalid cookiePolicy"因为我试图通过直接访问本地文件(index.html)来测试API。Google Sign In API仅在运行的web服务器中工作,正如以下文章(点击这里)所回答的那样
我假设它是身份验证函数中的signIn。你可以试着在返回之前做console.log(gapi.auth2.getAuthInstance())
看看返回的是什么。也许能给你点提示。