PeopleAPI 错误 403 Google 错误与否 [英] PeopleAPI Error 403 Google bugs or not
问题描述
我按照 PeopleAPI 示例尝试了 CreateContact https://developers.google.com/people/v1/write-people 但总是收到错误 403 Insufficient Authentication Scopes.我已经将范围设置为PeopleServiceService.Scope.Contacts
I tried CreateContact following PeopleAPI exmples https://developers.google.com/people/v1/write-people but always got error 403 Insufficient Authentication Scopes. I already set the scope to PeopleServiceService.Scope.Contacts
下面是我的完整代码
string[] Scopes = new string[] { PeopleServiceService.Scope.Contacts };
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets
{
ClientId = "xxxxxxx.apps.googleusercontent.com",
ClientSecret = "xxxxx"
},
Scopes,
"me",
System.Threading.CancellationToken.None).Result;
var peopleService = new Google.Apis.PeopleService.v1.PeopleServiceService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Test1"
});
try
{
//Create New COntact
Person contactToCreate = new Person();
List<Name> names = new List<Name>();
names.Add(new Name() { GivenName = "a1test1", FamilyName = "zzz" });
contactToCreate.Names = names;
Google.Apis.PeopleService.v1.PeopleResource.CreateContactRequest request =
new Google.Apis.PeopleService.v1.PeopleResource.CreateContactRequest(peopleService, contactToCreate);
Person createdContact = request.Execute();
}
catch (Exception merr)
{
MessageBox.Show(merr.Message);
}
有什么帮助吗?
TIA
推荐答案
这是我的人员服务初始化代码.
Here is my people service init code.
我的电话看起来像这样,但它有更多的代码 - 无法全部发布:
My call looks like this, but it has lot more code - can't post it all:
public static string[] scopes = { PeopleServiceService.Scope.Contacts };
GoogleUtils.People people = new People();
people.InitializePeopleService(scopes, userEmail);
运行 InitializePeopleService 然后你就可以使用 PeopleService.
Run InitializePeopleService then you are good to use PeopleService.
using Google.Apis.PeopleService.v1;
//using Google.Apis.PeopleService.v1.Data;
public static string[] scopes = { PeopleServiceService.Scope.Contacts };
private static PeopleServiceService peopleService;
public void InitializePeopleService(string[] scopes, string impersonateAs, string clientSecretFilePath = "client_secret_svc.json")
{
PeopleService = new PeopleServiceService(new BaseClientService.Initializer()
{
HttpClientInitializer = Auth.GetServiceAccountAuthorization(scopes: scopes, clientSecretFilePath: clientSecretFilePath, impersonateAs: impersonateAs)
});
if (PeopleService.Name != null)
CurrentPersonEmail = impersonateAs;
else
{
throw new Exception($"Failed to impersonate as {impersonateAs}");
}
}
public static PeopleServiceService PeopleService
{
get
{
if (peopleService == null)
{
throw new Exception("Please initialize the service by calling InitializePeopleService.");
}
return peopleService;
}
set
{
peopleService = value;
}
}
这篇关于PeopleAPI 错误 403 Google 错误与否的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!