使用 google-api-dotnet-client 的 Google+ 插入时刻 [英] Google+ insert moment using google-api-dotnet-client
问题描述
我正在尝试使用 dotnet-client 在 Google+ 中编写活动.问题是我似乎无法正确获取客户端应用程序的配置.根据 Google+ 登录配置 和 这个问题 我们需要添加 requestvisibleactions 参数.我这样做了,但没有用.我正在使用范围 https://www.googleapis.com/auth/plus.login
,我什至添加了范围 https://www.googleapis.com/auth/plus.moment.write
但插入仍然无效.
I am trying to write an activity in Google+ using the dotnet-client. The issue is that I can't seem to get the configuration of my client app correctly. According to the Google+ Sign-In configuration and this SO question we need to add the requestvisibleactions parameter. I did that but it did not work. I am using the scope https://www.googleapis.com/auth/plus.login
and I even added the scope https://www.googleapis.com/auth/plus.moments.write
but the insert still did not work.
这是我的请求网址的样子:
This is what my request url looks like:
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-1fbe06f1c6amp;ltf=弹出&安培; SHDF = Cm4LEhF0aGlyZFBhcnR5TG9nb1VybBoADAsSFXRoaXJkUGFydHlEaXNwbGF5TmFtZRoHQ2hpa3V0bwwLEgZkb21haW4aB0NoaWt1dG8MCxIVdGhpcmRQYXJ0eURpc3BsYXlUeXBlGgdERUZBVUxUDBIDbHNvIhTeWybcoJ9pXSeN2t-k8A4SUbfhsygBMhQivAmfNSs_LkjXXZ7bPxilXgjMsQ&安培; SCC = 1 代码>
正如你从那里看到的,有一个 request_visible_actions
,我什至添加了一个没有下划线的,以防我把参数弄错了(requestvisibleactions
).
As you can see from there that there is a request_visible_actions
and I even added one that has no underscore in case I got the parameter wrong (requestvisibleactions
).
让我说我的应用程序已通过 API 成功验证.我可以在通过身份验证后获取用户的个人资料,这是我的应用程序失败的插入时刻"部分.我的插入代码:
Let me say that my app is being authenticated successfully by the API. I can get the user's profile after being authenticated and it is on the "insert moment" part that my app fails. My insert code:
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)
{
} `
推荐答案
啊,就是这么简单!也许不会?我正在回答我自己的问题并因此接受它作为答案(当然是在几天之后),因此可以指导其他有相同问题的人.但我肯定会赞成 Gus 的回答,因为它引导我修复了我的代码.
Ah it's that simple! Maybe not? I am answering my own question and consequently accept it as the answer (after a few days of course) so others having the same issue may be guided. But I will definitely up-vote Gus' answer for it led me to the fix for my code.
所以根据上面写的@class 答案和 在他的博客中解释 成功创建时刻的关键是添加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/&request_visible_actions=http://schemas.google.com/AddActivity&access_type=offline.
So according to @class answer written above and as explained on his blog the key to successfully creating a moment is adding the request_visible_actions
parameter. I did that but my request still failed and it is because I was missing an important thing. You need to add one more parameter and that is the access_type
and it should be set to offline
. The OAuth request, at a minimum, should look like: https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/plus.login&response_type=code&redirect_uri=http://localhost/&request_visible_actions=http://schemas.google.com/AddActivity&access_type=offline.
对于完整且正确的客户端代码,您可以在此处获取 Gus 的示例 或 下载整个 dotnet 客户端库,包括源代码和示例 并添加我添加的内容以下.您应该记住的最重要的事情是为 Google API 修改您的 AuthorizationServerDescription
.这是我的身份验证器版本:
For the complete and correct client code you can get Gus' example here or download the entire dotnet client library including the source and sample and add what I added below. The most important thing that you should remember is modifying your AuthorizationServerDescription
for the Google API. Here's my version of the authenticator:
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
,我的代码永远不会工作,也永远不会工作.现在我想知道为什么?最好有一些解释.
Without the access_type=offline
my code never worked and it will never work. Now I wonder why? It would be good to have some explanation.
这篇关于使用 google-api-dotnet-client 的 Google+ 插入时刻的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!