为什么谷歌API不工作在我的通用Windows应用程序

Why does the Google API not work in my Universal Windows App?

本文关键字:Windows 应用程序 我的 谷歌 API 工作 为什么      更新时间:2023-09-26

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

你好,我拼命想让这个在我的通用Windows应用程序上工作。有一个谷歌URL缩短器的样本,说明如何使用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缩短器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应用程序中使用谷歌api吗?

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

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