2013-05-07 66 views
0

我試圖獲取Google Apps域的組織名稱。爲此,我使用Google Apps管理設置API。我看到它需要3腿OAuth。我嘗試實施OAuth 2.0,因爲OAuth 1已被棄用。我嘗試了很多事情來完成這項工作,但我總是得到一個401未受教育的人。Google Apps管理設置API - 401

我請求令牌範圍:https://apps-apis.google.com/a/feeds/domain/

這裏是我的代碼:

// 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); 
} 

我在做什麼錯? 這與OAuth 2兼容嗎?

我也想問問是否有另一種方式來獲得組織的名字,因爲庫的GData被認爲是過時的和新Google.Apis取代...

解決!

謝謝傑伊。它適用於OAuth 2.0遊樂場。我身邊的東西沒有正確設置。

使用Fiddler我看到授權標頭由我的應用程序設置。它被設置爲OAuth v1而不是V2。所以我發現我使用了錯誤的RequestFactory類。 需要使用的,而不是GOAuthRequestFactory GOAuth2RequestFactory ...

所以這是現在的工作:

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; 
+0

您是否註冊過特定範圍的應用程序?你不能爲你的應用沒有註冊的範圍請求訪問令牌。 – divyanshm 2013-05-07 15:32:38

回答

0

你使用了正確的API。儘管GData正在被新的Google API取代,但管理設置API現在仍然使用舊的GData格式。

您是否使用超級管理員帳戶進行身份驗證?你可以試試OAuth 2.0 playground的操作,看看它是否適用於那裏的賬戶?

您還可以查看Dito GAM,開源的Google Apps工具如何實現this call。如果您在與GAM相同的路徑中創建名爲debug.gam的文件,則GAM將打印出所有原始HTTP調用以及它正在進行的響應。

+0

謝謝。 OAuth 2.0遊樂場幫助我驗證我的請求,是的,我擁有超級管理員權限。 – Francis 2013-05-07 18:52:24

相關問題