我正在尝试使用dotnet-client在Google+中撰写活动。问题是我似乎无法正确获取客户端应用程序的配置。根据{{3}}和Google+ Sign-In configuration,我们需要添加requestvisibleactions参数。我这样做但是没有用。我正在使用范围https://www.googleapis.com/auth/plus.login
,我甚至添加了范围https://www.googleapis.com/auth/plus.moments.write
,但插入仍然无效。
这是我的请求网址:
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
,我甚至添加了一个没有下划线的情况,以防我得到错误的参数(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)
{
} `
答案 0 :(得分:2)
对于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>
假设您有一个在data-requestvisibleactions中设置了正确活动类型的PlusService对象,您应该能够复制/粘贴以查看它的以下代码,使用.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。
答案 1 :(得分:1)
啊,就这么简单!也许不吧?我正在回答我自己的问题,因此接受它作为答案(在课程结束几天之后),因此可能会引导具有相同问题的其他人。但我肯定会对Gus的回答进行投票,因为它导致我修复了我的代码。
因此,根据上面写的@class答案和explained on his blog,成功创建片刻的关键是添加request_visible_actions
参数。我做到了,但我的要求仍然失败,这是因为我错过了一件重要的事情。您需要再添加一个参数,即 access_type
,并且应将其设置为 offline
。 OAuth请求至少应如下所示: https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/plus.login&response_type=code&redirect_uri=http://localhost/ &amp; request_visible_actions = http://schemas.google.com/AddActivity&access_type=offline 。
对于完整且正确的客户端代码,您可以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
,我的代码永远不会工作,它永远不会有用。现在我想知道为什么?有一些解释会很好。