javascript - Uncaught ReferenceError : init is not defined

标签 javascript

我是 javascript 的新手,想学习一些东西,我一直在想,我在 https://developers.google.com/api-client-library/javascript/features/cors 上找到了这个我可以在哪里使用cors,这样我就可以访问像kissflow这样的谷歌API,我不知道我是否以正确的方式。所以事情就是这样,我使用的是上述站点中描述的独立身份验证客户端,但每次我尝试运行程序时都会出现错误提示Uncaught ReferenceError: init is not defined
我刚刚复制了网站上的代码

<script src="https://apis.google.com/js/api.js"
type="text/javascript">
</script>`<script type="text/javascript">`
//<![CDATA[gapi.load('auth', init);//]]>
</script>

最佳答案

尝试这个:

<script src="https://apis.google.com/js/api.js"
type="text/javascript">
</script>
<script>
gapi.load("client:auth2", function() {
  gapi.auth2.init({client_id: "<YOUR_CLIENT_ID>"});
});
</script>
确保您在本地托管它,并且在创建客户端 ID 时,将 localhost URL 添加到端口。如果没有 URL,客户端 ID 将不会发回正确的响应,从而导致错误。由于客户端ID不支持“file:///”,你必须使用网络托管服务或其他更简单的方法,只需下载最新的python并设置一个localhost服务器。
最新python下载:https://www.python.org/downloads/
设置服务器:https://developer.mozilla.org/en-US/docs/Learn/Common_questions/set_up_a_local_testing_server#running_a_simple_local_http_server
请注意,有时您必须使用“py”而不是“python”,因为我懒得研究😊。
要实际启动登录并使用 API:
<script>
function authenticate() {
return gapi.auth2.getAuthInstance().signIn({scope: ""/*Declare scopes with spaces in between, depends on API you're using*/})
  .then(function() { console.log("Sign-in successful"); },
  function(err) { console.error("Error signing in", err); });
}

function loadClient() {
  gapi.client.setApiKey("<YOUR_API_KEY>");
  return gapi.client.load(""/*Declare discovery document, depends on API you're using*/)
  .then(function() { console.log("GAPI client loaded for API"); },
  function(err) { console.error("Error loading GAPI client for API", err); });
}

function execute() {
  return gapi.client.classroom.courses.list({}) //Used classroom courses list as an example, but use the apropriate API Fields, found in your method's "Overview" section on the API Documentation
  .then(function(response) {
    // Handle the results here (response.result has the parsed body).
    console.log("Response", response);
  },
  function(err) { console.error("Execute error", err); });
}

gapi.load("client:auth2", function() {
  gapi.auth2.init({client_id: "<YOUR_CLIENT_ID>"});
});

authenticate().then(loadClient).then(execute)
</script>
请注意,有时您必须清除缓存才能使其正常工作(我的意思是,为我工作),所以如果您遇到问题,请尝试一下。

关于javascript - Uncaught ReferenceError : init is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31847457/

相关文章:

javascript - 如何向底层库提供 db、logger 等依赖项?

javascript - 取消选中时更改复选框样式

javascript - CodeSandbox 出现 Material-UI 错误 : undefined is not an object (evaluating '_context$muiTheme.borderRadius' )

javascript - ES6 双重解构

javascript - JSON 模式中的自定义属性

javascript - 展开/折叠 xslt 中的嵌套节点

javascript - 如何在vuejs中设置cookie?

javascript - 如何使用两个按钮调用和隐藏列表 - 使用 Javascript HTML CSS?

javascript - 网格中的 Kendo DropDownList 在选择后才绑定(bind)

javascript - angular js中$scope的可变范围如何工作?