我正在使用这个简单的服务器端代码来创建一个google身份验证令牌,该令牌将在客户端用于嵌入视图
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;
}
}
我使用页面上的令牌来授权API
gapi.analytics.ready(function() {
gapi.analytics.auth.authorize({
'serverAuth': {
'access_token': '{{ <%=bearer%> }}'
}
});
但我收到错误401“Invalide Credentials”:
{"error":{"errors":[{"domain":"global","reason":"authError","message":"Invalid Credentials","locationType":"header","location":"Authorization"}],"code":401,"message":"Invalid Credentials"}}
在https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=上测试了令牌值 它看起来有效
Google帐户似乎已使用具有所有必需权限的服务帐户正确设置
我在这里缺少什么?
答案 0 :(得分:0)
好吧,发现了愚蠢的复制粘贴问题 我复制并粘贴了这个:
gapi.analytics.ready(function() {
gapi.analytics.auth.authorize({
'serverAuth': {
'access_token': '{{ <%=bearer%> }}'
}
});
大括号是问题,它们只是占位符,应该被删除,所以这样做:
gapi.analytics.ready(function() {
gapi.analytics.auth.authorize({
'serverAuth': {
'access_token': '<%=bearer%>'
}
});