【发布时间】:2014-12-14 19:03:19
【问题描述】:
根据Moments.insert 的文档,需要具有以下范围的 Google+ API 身份验证
https://www.googleapis.com/auth/plus.login
我正在使用所有可能的 PlusService 范围进行身份验证,但我仍然收到以下错误
Google.Apis.Requests.RequestError 未经授权 [401] 错误 [ 消息[未授权]位置[-]原因[未授权] 域[全局]
//Scopes for use with Google+ API
// activating Google+ API in console
// Documentation: https://developers.google.com/+/api/oauth
string[] scopes = new string[] {
PlusService.Scope.PlusLogin,
PlusService.Scope.UserinfoEmail,
PlusService.Scope.UserinfoProfile
};
string _client_id = "2046123799103-d0vpdthl4ms0soutcrpe036ckqn7rfpn.apps.googleusercontent.com";
string _client_secret = "NDmluNfTgUk6wgmy7cFo64RV";
PlusService service = null;
UserCredential credential = null;
try {
// here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets {
ClientId = _client_id, ClientSecret = _client_secret
},
scopes,
Environment.UserName,
CancellationToken.None,
new FileDataStore("Daimto.GooglePlus.Auth.Store")).Result;
} catch (Exception ex) {
//If the user hits cancel you wont get access.
if (ex.InnerException.Message.IndexOf("access_denied") != -1) {
Console.WriteLine("User declined access");
Console.ReadLine();
return;
} else {
Console.WriteLine("Unknown Authentication Error:" + ex.Message);
Console.ReadLine();
return;
}
}
// Now we create a Google service. All of our requests will be run though this.
service = new PlusService(new BaseClientService.Initializer() {
HttpClientInitializer = credential,
ApplicationName = "Google Plus Sample",
});
Moment body = new Moment();
body.Type = "http://schema.org/AddAction";
ItemScope itemScope = new ItemScope();
itemScope.Id = "target-id-1";
itemScope.Type = "http://schema.org/AddAction";
itemScope.Name = "The Google+ Platform";
itemScope.Description = "A page that describes just how awesome Google+ is!";
itemScope.Image = "https://developers.google.com/+/plugins/snippet/examples/thing.png";
body.Object = itemScope;
try {
var l = service.Moments.Insert(body, "me", MomentsResource.InsertRequest.CollectionEnum.Vault);
l.Execute();
} catch (Exception ex) {
int i = 1;
}
我已经测试了身份验证并且它正在工作我能够列出活动和其他内容。它唯一的插入时刻给了我这个错误。我也尝试在 PHP 中执行此操作,但遇到了同样的错误。我错过了什么?
更新:我在 moments.insert 的文档中找到了一些东西
在对 moment.insert 进行身份验证时,您必须包含 data-requestvisibleactions参数指定哪些类型的App 您的应用程序将编写的活动。
我还没有弄清楚如何设置这个 data-requestvisibleactions。
【问题讨论】:
-
这很奇怪。我同意。尝试添加"profile" scope。 documentation 中提到 UserInfoEmail 和 UserInfoProfile 都已弃用。
-
@peleyal 配置文件没有作为 PlusService.Scope 中的范围出现,客户端库中可能缺少它吗?你知道什么是 data-requestvisibleactions 吗?
-
看起来它要与 java 一起工作你必须做一些 hackie 将它添加到 url developers.google.com/+/web/app-activities
-
您可以添加“profile”(字符串)并检查一下吗?它最终对你有用吗?我的意思是使用您刚刚分享的链接...
-
不,我还没有让它工作。 “配置文件”范围对同样的错误没有帮助。
标签: c# google-plus google-api-dotnet-client