Not sure, what I am doing wrong. I am getting this error message
{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Login Required",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Login Required"
}
}
I have logged in and then calling the google prediction API. Will appreciate all help. Here is the code I am trying with. Please note I have changed the Client Id and API key to some invalid values.
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
</head>
<body>
<!--Add a button for the user to click to initiate auth sequence -->
<button id="authorize-button" style="visibility: hidden">Authorize</button>
<button type="button" onclick="runprediction()">Predict</button>
<script type="text/javascript">
// Enter a client ID for a web application from the Google Developer Console.
// The provided clientId will only work if the sample is run directly from
// https://google-api-javascript-client.googlecode.com/hg/samples/authSample.html
// In your Developer Console project, add a JavaScript origin that corresponds to the domain
// where you will be running the script.
var clientId = '203416138702-oqafvmbj4j9db3gqgvtsuo2lpcnad6jp.apps.googleusercontent.com'; //OAuth2.0 Client Id (Changed the actual key)
// Enter the API key from the Google Develoepr Console - to handle any unauthenticated
// requests in the code.
// The provided key works for this sample only when run from
// https://google-api-javascript-client.googlecode.com/hg/samples/authSample.html
// To use in your own application, replace this API key with your own.
var apiKey = 'BxBAsyD8qFXc-2Z7IPRD05SV90G72EEx99nxiwE'; //Browser Key (Changed the actual key)
// To enter one or more authentication scopes, refer to the documentation for the API.
var scopes = ['https://www.googleapis.com/auth/plus.me' , 'https://www.googleapis.com/auth/prediction']
// Use a button to handle authentication the first time.
function handleClientLoad() {
gapi.client.setApiKey(apiKey);
window.setTimeout(checkAuth,1);
}
function checkAuth() {
gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: true}, handleAuthResult);
}
function handleAuthResult(authResult) {
var authorizeButton = document.getElementById('authorize-button');
if (authResult && !authResult.error) {
authorizeButton.style.visibility = 'hidden';
makeApiCall();
} else {
authorizeButton.style.visibility = '';
authorizeButton.onclick = handleAuthClick;
}
}
function handleAuthClick(event) {
gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthResult);
return false;
}
// Load the API and make an API call. Display the results on the screen.
function makeApiCall() {
gapi.client.load('plus', 'v1', function() {
var request = gapi.client.plus.people.get({
'userId': 'me'
});
request.execute(function(resp) {
var heading = document.createElement('h4');
var image = document.createElement('img');
image.src = resp.image.url;
heading.appendChild(image);
heading.appendChild(document.createTextNode(resp.displayName));
document.getElementById('content').appendChild(heading);
});
});
}
</script>
<script src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
runprediction = function() {
var resultDiv = $("#resultDivContainer");
$.ajax({
url: "https://www.googleapis.com/prediction/v1.6/projects/314525027691/trainedmodels/myopcodeprediction20151104/predict?key={AIzaSyD8qFXc-2Z7IPRD05SV90G72EEx99nxiwE}",
type: "POST",
data: {"input": {"csvInstance": ["01053","DCSS","ADVISED CUSTOMER VEHICLE NEEDS COOLING SYSTEM SERVICE"]}},
dataType: "json",
success: function (data, status, jqXHR) {
alert("success");
},
error: function (jqXHR, status) {
console.log(jqXHR);
alert('fail...:' + status.code);
}
});
};
</script>
<div id="content"></div>
<p>Retrieves your profile name using the Google Plus API.</p>
<div id="resultDivContainer"></div>
</body>
</html>