【问题标题】:Create Contacts with Google API exception - Write Access denied使用 Google API 异常创建联系人 - 写入访问被拒绝
【发布时间】:2018-07-02 12:34:30
【问题描述】:
我正在尝试在 ASP .NET Core (c#) 中使用 Google API 创建 Google 联系人。但是,它引发了一个异常,例如:
对 gmail 的写入权限被拒绝。
我正在使用https://www.google.com/m8/feeds/contacts/ 获取/发布。找不到我失踪的地方。
是否可以使用普通的 gmail 帐户创建联系人。我应该遵循哪些步骤?
提前致谢
【问题讨论】:
标签:
c#
google-api
google-contacts-api
【解决方案1】:
对 gmail 的写入权限被拒绝。
表示您无权写入用户帐户。
联系人数据 API 提供两种类型的提要:联系人提要和联系人组提要。
提要是私有的读/写提要,可用于查看和管理用户的联系人/群组。由于它们是私有的,因此程序员只能通过使用经过身份验证的请求来访问它们。也就是说,请求必须包含您要检索其联系人的用户的身份验证令牌。有关身份验证的更多信息,请参阅developer's guide。
您可能还需要考虑以下问题。这可能会让你的生活更轻松,而不是使用这个旧的 api。
- 您应该使用 google people api。
- 您应该使用 google .net 客户端库。
- 您需要经过身份验证才能将数据插入用户联系人。
授权示例
private static UserCredential GetUserCredential(string clientSecretJson, string userName, string[] scopes)
{
try
{
if (string.IsNullOrEmpty(userName))
throw new ArgumentNullException("userName");
if (string.IsNullOrEmpty(clientSecretJson))
throw new ArgumentNullException("clientSecretJson");
if (!File.Exists(clientSecretJson))
throw new Exception("clientSecretJson file does not exist.");
// These are the scopes of permissions you need. It is best to request only what you need and not all of them
using (var stream = new FileStream(clientSecretJson, FileMode.Open, FileAccess.Read))
{
string credPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials/", System.Reflection.Assembly.GetExecutingAssembly().GetName().Name);
// Requesting Authentication or loading previously stored authentication for userName
var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets,
scopes,
userName,
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
credential.GetAccessTokenForRequestAsync();
return credential;
}
}
catch (Exception ex)
{
throw new Exception("Get user credentials failed.", ex);
}
}
Oauth2Authentication.cs