我正在尝试用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,我的代码永远不会工作,也永远不会工作。现在我想知道为什么?如果能有一些解释就好了。
发布于 2013-03-08 10:30:18
对于OAuth流,您的请求有两个问题:
中传递
确保您的项目引用此处提供的最新版本的Google+ .NET客户端库:
https://developers.google.com/resources/api-libraries/download/stable/plus/v1/csharp
我在GitHub上创建了一个项目,在这里显示了一个完整的服务器端流程:
https://github.com/gguuss/gplus_csharp_ssflow
正如Brettj所说,您应该使用Google+登录按钮,如此处提供的最新Google+示例所示:
https://github.com/googleplus/gplus-quickstart-csharp
首先,确保您请求的是您正在编写的所有活动类型。您将知道这是有效的,因为授权对话框将显示“通过Google使您的应用程序活动可用,对您可见,并且:...”下面的文本以“此应用程序想要”开头。我知道你已经检查过了,但我有90%的把握这就是你得到401错误代码的原因。以下标记显示了如何呈现请求访问以添加活动的Google+登录按钮。
<div id="gConnect">
<button class="g-signin"
data-scope="https://www.googleapis.com/auth/plus.login"
data-requestvisibleactions="http://schemas.google.com/AddActivity"
data-clientId="YOUR_CLIENT_ID"
data-accesstype="offline"
data-callback="onSignInCallback"
data-theme="dark"
data-cookiepolicy="single_host_origin">
</button>假设你有一个PlusService对象,在data-requestvisibleactions中设置了正确的活动类型,下面的代码,你应该能够复制/粘贴来查看它的工作,简要地演示了使用.NET客户端编写时刻,并且已经过测试:
Moment body = new Moment();
ItemScope target = new ItemScope();
target.Id = "replacewithuniqueforaddtarget";
target.Image = "http://www.google.com/s2/static/images/GoogleyEyes.png";
target.Type = "";
target.Description = "The description for the activity";
target.Name = "An example of add activity";
body.Target = target;
body.Type = "http://schemas.google.com/AddActivity";
MomentsResource.InsertRequest insert =
new MomentsResource.InsertRequest(
_plusService,
body,
"me",
MomentsResource.Collection.Vault);
Moment wrote = insert.Fetch();注意,为了方便起见,我包含了Google.Apis.Plus.v1.Data。
https://stackoverflow.com/questions/15263378
复制相似问题