未捕获的ReferenceError:未定义init

Kur*_*roi 5 javascript cors

我是一个javascript的新手,想要学习一些东西,我一直想知道,我在https://developers.google.com/api-client-library/javascript/features/cors上找到了这个,我可以使用cors,所以我可以访问像kissflow一样的谷歌API,我不知道我是否以正确的方式.所以这就是我在使用上述网站中描述的独立Auth客户端,但每次我都试图运行程序时出现错误提示

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>
Run Code Online (Sandbox Code Playgroud)

End*_*gon -1

尝试这个:

<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>
Run Code Online (Sandbox Code Playgroud)

确保在本地托管它,并在创建客户端 ID 时,添加本地主机 URL 和端口。如果没有 URL,客户端 ID 将无法发回正确的响应,从而导致错误。由于客户端 ID 不支持“file:///”,因此您必须使用网络托管服务或其他服务,或者更简单的方法,只需下载最新的 python 并设置本地主机服务器。

最新的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>
Run Code Online (Sandbox Code Playgroud)

请注意,有时您必须清除缓存才能使其正常工作(我的意思是,对我有用),所以如果您遇到一些麻烦,请尝试一下。