我按照PeopleAPI示例https://developers.google.com/people/v1/write-people尝试了CreateContact,但总是得到错误403身份验证范围不足。我已经将作用域设置为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);
}有什么需要帮忙的吗?
提亚
发布于 2021-06-08 04:08:36
这是我的people服务初始化代码。
我的调用看起来像这样,但它有更多的代码-不能全部发布:
public static string[] scopes = { PeopleServiceService.Scope.Contacts };
GoogleUtils.People people = new People();
people.InitializePeopleService(scopes, userEmail);运行InitializePeopleService,然后就可以使用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;
}
}https://stackoverflow.com/questions/50385783
复制相似问题