我正在尝试用Google+编写一个使用dotnet客户端的活动。问题是我似乎不能正确获得我的客户端应用程序的配置。根据Google+ Sign-In configuration和this SO question,我们需要添加requestvisibleactions参数。我这样做了,但它不起作用。我正在使用作用域https://www.googleapis.com/auth/plus.login,甚至添加了作用域https://www.googleapis.com/auth/plus.moments.write,但插入仍然不起作用。
下面是我的请求url:
https://accounts.google.com/ServiceLogin?service=lso&passive=1209600&continue=https://accounts.google.com/o/oauth2/auth?scope%3Dhttps://www.googleapis.com/auth/plus.login%2Bhttps://www.googleapis.com/auth/plus.moments.write%26response_type%3Dcode%26redirect_uri%3Dhttp://localhost/%26state%3D%26requestvisibleactions%3Dhttp://schemas.google.com/AddActivity%26client_id%3D000.apps.googleusercontent.com%26request_visible_actions%3Dhttp://schemas.google.com/AddActivity%26hl%3Den%26from_login%3D1%26as%3D-1fbe06f1c6120f4d<mpl=popup&shdf=Cm4LEhF0aGlyZFBhcnR5TG9nb1VybBoADAsSFXRoaXJkUGFydHlEaXNwbGF5TmFtZRoHQ2hpa3V0bwwLEgZkb21haW4aB0NoaWt1dG8MCxIVdGhpcmRQYXJ0eURpc3BsYXlUeXBlGgdERUZBVUxUDBIDbHNvIhTeWybcoJ9pXSeN2t-k8A4SUbfhsygBMhQivAmfNSs_LkjXXZ7bPxilXgjMsQ&scc=1
从那里可以看到,有一个request_visible_actions,我甚至添加了一个没有下划线的has,以防我把参数弄错了(requestvisibleactions)。
我要说的是,我的应用程序正在成功地通过API的身份验证。我可以在认证后获得用户的个人资料,这是在“插入时刻”部分,我的应用程序失败。我的插入代码:
var body = new Moment();
var target = new ItemScope();
target.Id = referenceId;
target.Image = image;
target.Type = "http://schemas.google.com/AddActivity";
target.Description = description;
target.Name = caption;
body.Target = target;
body.Type = "http://schemas.google.com/AddActivity";
var insert =
new MomentsResource.InsertRequest(
// this is a valid service instance as I am using this to query the user's profile
_plusService,
body,
id,
MomentsResource.Collection.Vault);
Moment result = null;
try
{
result = insert.Fetch();
}
catch (ThreadAbortException)
{
// User was not yet authenticated and is being forwarded to the authorization page.
throw;
}
catch (Google.GoogleApiRequestException requestEx)
{
// here I get a 401 Unauthorized error
}
catch (Exception ex)
{
} `发布于 2013-03-08 15:48:46
啊,就这么简单!也许不是?我正在回答我自己的问题,并因此接受它作为答案(当然是在几天后),以便其他有同样问题的人可以得到指导。但我肯定会支持Gus的回答,因为它让我找到了代码的修复方法。
因此,根据上面写的@class答案,作为explained on his blog,成功创建时刻的关键是添加request_visible_actions参数。我这样做了,但我的请求仍然失败,这是因为我错过了一个重要的东西。您需要再添加一个参数,即access_type,该参数应设置为offline.OAuth请求至少应如下所示:&request_visible_actions=.
要获得完整和正确的客户端代码,您可以使用get Gus' example here或download the entire dotnet client library including the source and sample,然后添加我在下面添加的内容。您应该记住的最重要的事情是为Google API修改您的AuthorizationServerDescription。下面是我的验证器版本:
public static OAuth2Authenticator<WebServerClient> CreateAuthenticator(
string clientId, string clientSecret)
{
if (string.IsNullOrWhiteSpace(clientId))
throw new ArgumentException("clientId cannot be empty");
if (string.IsNullOrWhiteSpace(clientSecret))
throw new ArgumentException("clientSecret cannot be empty");
var description = GoogleAuthenticationServer.Description;
var uri = description.AuthorizationEndpoint.AbsoluteUri;
// This is the one that has been documented on Gus' blog site
// and over at Google's (https://developers.google.com/+/web/signin/)
// This is not in the dotnetclient sample by the way
// and you need to understand how OAuth and DNOA works.
// I had this already, see my original post,
// I thought it will make my day.
if (uri.IndexOf("request_visible_actions") < 1)
{
var param = (uri.IndexOf('?') > 0) ? "&" : "?";
description.AuthorizationEndpoint = new Uri(
uri + param +
"request_visible_actions=http://schemas.google.com/AddActivity");
}
// This is what I have been missing!
// They forgot to tell us about this or did I just miss this somewhere?
uri = description.AuthorizationEndpoint.AbsoluteUri;
if (uri.IndexOf("offline") < 1)
{
var param = (uri.IndexOf('?') > 0) ? "&" : "?";
description.AuthorizationEndpoint =
new Uri(uri + param + "access_type=offline");
}
// Register the authenticator.
var provider = new WebServerClient(description)
{
ClientIdentifier = clientId,
ClientSecret = clientSecret,
};
var authenticator =
new OAuth2Authenticator<WebServerClient>(provider, GetAuthorization)
{ NoCaching = true };
return authenticator;
}没有access_type=offline,我的代码永远不会工作,也永远不会工作。现在我想知道为什么?如果能有一些解释就好了。
https://stackoverflow.com/questions/15263378
复制相似问题