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

0

Error: 0x800a138f - JavaScript runtime error: Unable to get property 'setApiKey' of undefined or null reference

Hello I'm desperatly trying to get this to work on my Universal Windows App. There is a sample for Googles URL shortener with instructions on how to use the API.

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

All of this works when I run this in my Browser, but as soon as I start running this in my Universal Windows App, it won't work. I figured that it had something to do with the security of the UWP, inline skripts arent allowed and you can't load scripts from the web normally. You have to use a webview to load scripts from the web so I did this with this 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>

This is my URL Shortener html file:

<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>

Error: 0x800a138f - JavaScript runtime error: Unable to get property 'setApiKey' of undefined or null reference

I have no idea why that happens or what else I can do to fix this. Is it even possible to use the google api in windows apps???

javascript
windows
google-api
win-universal-app
uwp
asked on Stack Overflow Sep 10, 2016 by Victor Odukoya • edited Sep 11, 2016 by Victor Odukoya

1 Answer

0

Please check Rob's comments in your MSDN case: https://social.msdn.microsoft.com/Forums/windowsapps/en-US/bd101515-212b-4366-b60d-2807b2783f62

Rob mentioned two choices: calling local js files if Google permits it, or taking full control and stream your content to your x-ms-webview with navigateToLocalStreamUri method.

answered on Stack Overflow Sep 14, 2016 by Franklin Chen - MSFT • edited May 23, 2017 by Community

User contributions licensed under CC BY-SA 3.0