谷歌Apps管理设置API - 401 [英] Google Apps Admin Settings API - 401

查看:214
本文介绍了谷歌Apps管理设置API - 401的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为谷歌Apps域获得组织名称。对于这一点,我使用谷歌Apps管理设置API。我看到,它需要三足式OAuth。我尝试实施的OAuth 2.0,因为OAuth的1 pcated德$ P $。我尝试了很多事情来得到这个工作,但我总是得到一个401 unautorized。

我请求范围的标记: https://apps-apis.google .COM / A /供稿/域/

下面是我的code:

  //客户端ID和放大器; ClientSecret值
变种requestFactory = GDAPI.GoogleApps.GetAuthRequestFactory();字符串ORGANIZATIONNAME =的String.Empty; Google.GData.Apps.AdminSettings.AdminSettingsService服务=
            新Google.GData.Apps.AdminSettings.AdminSettingsService(auth.Domain,Excendia.Mobility.Utilities1.BLL.WebConfig.ExcendiaAppName);
 service.RequestFactory = requestFactory;
 service.SetAuthenticationToken(标记); 尝试
 {
     VAR的结果= service.GetOrganizationName(); //此处抛出异常...
 }
 赶上(异常前)
 {
     log.Error(除息);
 }

我是什么做错了吗?
是的OAuth 2这个兼容?

我也想问问是否有另一种方式来获得组织的名字,因为库的GData被认为是过时的和新的Google.Apis取代...

解决!

感谢周杰伦。它的工作原理上的OAuth 2.0游乐场。在我身边的东西是不正确。

使用招我看到了Authorization头被我的应用程序设置。它被设置到OAuth V1,而不是V2。所以我发现我用错了RequestFactory类。
需要使用的,而不是GOAuthRequestFactory ...

GOAuth2RequestFactory

所以这个现在工作:

 字符串ORGANIZATIONNAME =的String.Empty;Google.GData.Apps.AdminSettings.AdminSettingsService服务=
            新Google.GData.Apps.AdminSettings.AdminSettingsService(auth.DomainmyAppName);service.RequestFactory =
            新Google.GData.Client.GOAuth2RequestFactory(瘦肉精,MyAppName
            新Google.GData.Client.OAuth2Parameters()
            {客户端Id =客户端ID,
              ClientSecret = ClientSecret,
              的accessToken =令牌});尝试
{    VAR的结果= service.GetOrganizationName();    如果(结果!= NULL)
    {
        ORGANIZATIONNAME = result.OrganizationName;
    }
}
赶上(异常前)
{
    log.Error(除息);
}返回ORGANIZATIONNAME;


解决方案

您使用的是正确的API。虽然的GData正在被谷歌新的API更换,管理设置API仍然使用旧的GData格式了。

您使用超级管理员帐户进行身份验证?你可以尝试在的OAuth 2.0操场操作,看看它是否适用于该帐户吗?

您也可以看看如何 DITO GAM ,一个开源的谷歌Apps的工具器具<一href=\"https://$c$c.google.com/p/google-apps-manager/wiki/DomainSettingsExamples#Retrieving_Domain_Settings\"相对=nofollow>此调用。如果您创建一个作为GAM相同的路径命名debug.gam文件,GAM将打印出所有的原始HTTP调用,它使反应/获取。

I'm trying to get the Organization name for a Google Apps domain. For this, I'm using the Google Apps Admin Settings API. I saw that it required 3-Legged OAuth. I try to implement OAuth 2.0 because OAuth 1 is deprecated. I try many thing to get this work but I'm always getting a 401 unautorized.

I request a token for the scope : https://apps-apis.google.com/a/feeds/domain/

Here is my code:

// ClientID & ClientSecret values
var requestFactory = GDAPI.GoogleApps.GetAuthRequestFactory();

string organizationName = String.Empty;

 Google.GData.Apps.AdminSettings.AdminSettingsService service = 
            new Google.GData.Apps.AdminSettings.AdminSettingsService(auth.Domain, Excendia.Mobility.Utilities1.BLL.WebConfig.ExcendiaAppName);
 service.RequestFactory = requestFactory;
 service.SetAuthenticationToken(token);

 try
 {
     var result = service.GetOrganizationName(); // throw exception here...
 }
 catch (Exception ex)
 {
     log.Error(ex);
 }

What am I doing wrong? Is this compatible with OAuth 2?

I also want to ask if there is another way to get organization name because GData library is supposed to be obsolete and replaced by new Google.Apis...

Resolved!

Thanks Jay. It works on OAuth 2.0 playground. Something on my side was not set correctly.

Using Fiddler I saw the Authorization header being set by my application. It was set to OAuth v1 instead of v2. So I found out I was using the wrong RequestFactory class. Need to use GOAuth2RequestFactory instead of GOAuthRequestFactory...

So this is now working:

string organizationName = String.Empty;

Google.GData.Apps.AdminSettings.AdminSettingsService service = 
            new Google.GData.Apps.AdminSettings.AdminSettingsService(auth.Domain, "myAppName");

service.RequestFactory = 
            new Google.GData.Client.GOAuth2RequestFactory("cl", "MyAppName",
            new Google.GData.Client.OAuth2Parameters()
            { ClientId = ClientID, 
              ClientSecret = ClientSecret, 
              AccessToken = token });

try
{

    var result = service.GetOrganizationName();

    if (result != null)
    {
        organizationName = result.OrganizationName;
    }
}
catch (Exception ex)
{
    log.Error(ex);
}

return organizationName;

解决方案

You are using the correct API. Though GData is being replaced by the new Google APIs, Admin Settings API still uses the old GData format for now.

Are you using a super administrator account to authenticate with? Can you try the operation on the OAuth 2.0 playground and see if it works for the account there?

You can also take a look at how Dito GAM, an open source Google Apps tool implements this call. If you create a file named debug.gam in the same path as GAM, GAM will print out all the raw HTTP calls and responses it's making/getting.

这篇关于谷歌Apps管理设置API - 401的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆