无法使用Google Admin SDK创建用户 - 例外401未经授权

时间:2016-08-29 09:16:18

标签: c# dotnetopenauth google-admin-sdk

我正在尝试在我的C#应用​​程序中填充用户。但是,我一直得到这个例外:

<body>
  <div class="list">
    <a href="#"><i class="fa fa-fw fa-home"></i><span>test</span></a>
  </div><!--list-->
</body>

正在提出例外情况:

{"Error occurred while sending a direct message or getting the response."}
[DotNetOpenAuth.Messaging.ProtocolException]: {"Error occurred while sending a direct message or getting the response."}
Data: {System.Collections.ListDictionaryInternal}
HelpLink: null
InnerException: {"The remote server returned an error: (401) Unauthorized."}
Message: "Error occurred while sending a direct message or getting the response."
Source: "Google.Apis"
StackTrace: "   at Google.Apis.Requests.ClientServiceRequest`1.Execute() in c:\\code.google.com\\google-api-dotnet-client\\default\\Tools\\BuildRelease\\bin\\Release\\release140\\default\\Src\\GoogleApis\\Apis\\Requests\\ClientServiceRequest.cs:line 88\r\n   at GoogleAccountsPopulation.DirectoryServiceClient.CreateUser(User user) in E:\\GoogleAccountsPopulation\\GoogleAccountsPopulation\\GoogleAccountsPopulation\\DirectoryServiceClient.cs:line 70\r\n   at GoogleAccountsPopulation.frmGoogleAccountsPopulation.btnPopulateAccounts_Click(Object sender, EventArgs e) in E:\\GoogleAccountsPopulation\\GoogleAccountsPopulation\\GoogleAccountsPopulation\\PopulateForm.cs:line 449"
TargetSite: {TResponse Execute()}

以下是服务的代码:

userServiceClient.CreateUser(user);

1 个答案:

答案 0 :(得分:0)

授权

  

在Google Apps域中,域管理员可以向第三方应用授予对其用户的域范围访问权限。数据 - 这称为域范围的授权。要以这种方式委派权限,域管理员可以将服务帐户与OAuth 2.0一起使用。

以下是scopes available in Directory API

的列表
  

要使用OAuth 2.0请求访问权限,您的应用程序需要范围信息以及Google在您注册应用程序时提供的信息(例如客户端ID和客户端密钥)。

尝试检查perform Google Apps Domain-Wide delegation of authority的方式。您收到异常401未经授权可能是因为您的服务帐户无权创建用户(使用了错误的范围或未添加使用的范围),基于Emily's answer - 您必须冒充a 管理员帐户(注意:只有管理员可以创建用户)。