【问题标题】:Why does the Google API not work in my Universal Windows App?为什么 Google API 在我的通用 Windows 应用程序中不起作用?
【发布时间】:2017-01-18 15:07:58
【问题描述】:

错误:0x800a138f - JavaScript 运行时错误:无法获取未定义或空引用的属性“setApiKey”

您好,我正在拼命尝试让它在我的通用 Windows 应用程序上运行。有一个 Google 网址缩短器示例,其中包含有关如何使用 API 的说明。

https://developers.google.com/api-client-library/javascript/samples/samples

当我在我的浏览器中运行它时,所有这些都有效,但是一旦我开始在我的通用 Windows 应用程序中运行它,它就不起作用了。我认为这与 UWP 的安全性有关,不允许使用内联脚本,并且您无法正常从 Web 加载脚本。你必须使用 webview 从 web 加载脚本,所以我用这个 webview 做到了这一点:

<x-ms-webview id="UrlShortenerWebview"src="ms-appx-web:///Pages/URLShortener/URLShortener.html" style="width: 200px; height: 200px; border: 1px solid black;"></x-ms-webview>

这是我的 URL Shortener html 文件:

<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title></title>

	<script src="https://apis.google.com/js/api.js" type="text/javascript"></script>
	<script>
		function appendResults(text) {
			var results = document.getElementById('results');
			results.appendChild(document.createElement('P'));
			results.appendChild(document.createTextNode(text));
		}

		function makeRequest() {
			var request = gapi.client.urlshortener.url.get({
				'shortUrl': 'http://goo,gl/fbsS'
			});
			request.then(function (response) {
				appendResults(response.result.longUrl);
			}, function (reason) {
				console.log('Error: ' + reason.result.error.message);
			});
		}
		function init() {
			gapi.client.setApiKey('AIzaSyCzBnER6KmLiO2ZBIycZIPCEQEXxIrHnR0');
			gapi.client.load('urlshortener', 'v1').then(makeRequest)
		}
		gapi.load("client", init);
	</script>
</head>
<body>
	<div id="results"></div>
</body>
</html>

错误:0x800a138f - JavaScript 运行时错误:无法获取未定义或空引用的属性“setApiKey”

我不知道为什么会发生这种情况,也不知道我还能做些什么来解决这个问题。甚至可以在windows应用程序中使用google api???

【问题讨论】:

  • 这本质上是一个 NPE。调试您的代码并找出null 是如何进入您的代码的。
  • 我不知道怎么做。当我控制台日志 gapi.client 原来是未定义的。是因为 gapi.load 函数,没有创建客户端对象吗?即使我不使用 gapi.load 函数而只使用 ?onload=init 加载 api,我也会收到相同的错误消息

标签: javascript windows google-api win-universal-app uwp


【解决方案1】:

请在您的 MSDN 案例中查看 Rob 的 cmets:https://social.msdn.microsoft.com/Forums/windowsapps/en-US/bd101515-212b-4366-b60d-2807b2783f62

Rob 提到了两种选择:如果 Google 允许,则调用本地 js 文件,或者使用 navigateToLocalStreamUri 方法完全控制并将您的内容流式传输到您的 x-ms-webview

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-02
    • 1970-01-01
    相关资源
    最近更新 更多