I am using this simple server-side code to create a google authentication token that to be used on the client side for embedding a view
private void GenarateGAToken()
{
GoogleCredential credential;
string keyFilePath = Server.MapPath("") + MYKEYFILENAME;
using (Stream stream = new FileStream(keyFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
credential = GoogleCredential.FromStream(stream);
}
string[] scopes = new string[] { AnalyticsService.Scope.Analytics }; credential = credential.CreateScoped(scopes);
try
{
bearer = ((ITokenAccess)credential).GetAccessTokenForRequestAsync().Result;
}
catch (AggregateException ex)
{
throw ex.InnerException;
}
}
I use the token on the page to authorise the API
gapi.analytics.ready(function() {
gapi.analytics.auth.authorize({
'serverAuth': {
'access_token': '{{ <%=bearer%> }}'
}
});
But I get back error 401 "Invalide Credentials":
{"error":{"errors":[{"domain":"global","reason":"authError","message":"Invalid Credentials","locationType":"header","location":"Authorization"}],"code":401,"message":"Invalid Credentials"}}
Tested the token value on https://www.googleapis.com/oauth2/v1/tokeninfo?access_token= And it looks valid
The Google account seems to be set correctly with a service account with all required permissions
What am I missing here?
Well, found the stupid copy-paste problem I copied and pasted this:
gapi.analytics.ready(function() {
gapi.analytics.auth.authorize({
'serverAuth': {
'access_token': '{{ <%=bearer%> }}'
}
});
The curly brackets are the problem, they are just placeholder and should be removed, so this works:
gapi.analytics.ready(function() {
gapi.analytics.auth.authorize({
'serverAuth': {
'access_token': '<%=bearer%>'
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.