2016-05-19 10 views
1

私はSharePoint - O365上でイントラネットを作成しています。ここでカレンダーイベントを取得して1週間表示する必要があるウィジェットを作成できます。手順は次のとおりです。SharePoint AppのOauth2とアクセストークン

a。ユーザーはイントラネットにログインします b。アクセストークンが生成され、Office 365 REST API にアクセスします。カレンダーイベントが取得され、表示されます。ユーザーコンテキストをaccpets WCFアプリケーションを作成し、トークンを生成:

は、私がアクセストークンに

オプションを生成するために、2つのオプションを考えた:

は、ここに私の問題です。これにより、結果が取得され、リストが更新されます。私のイントラネットアプリケーションはカレンダーリストを読み、ウィジェットを更新できます。これは、SPからWCFメソッドにユーザーコンテキストを渡すことができなかったため、アクセストークンを生成することができませんでした。

オプションb:次のコードを使用します(これは私が今行っていますが)クライアントにはうまくいかないURLにアクセストークンを表示します。

var clientId = '>> sample >>';

var replyUrl = '<<>>'; 
var endpointUrl = 'https://outlook.office365.com/api/v1.0/me/events'; 
var resource = "https://outlook.office365.com/"; 

var authServer = 'https://login.windows.net/common/oauth2/authorize?'; 
var responseType = 'token'; 


var url = authServer + 
     "response_type=" + encodeURI(responseType) + "&" + 
     "client_id=" + encodeURI(clientId) + "&" + 
     "resource=" + encodeURI(resource) + "&" + 
     "redirect_uri=" + encodeURI(replyUrl); 

window.location = url;

これを達成する他の方法はありますか?

Ankush

答えて

0

あなたはWCFを使用することを述べているので、あなたが提供するホストのSharePointアプリを開発していますか?

私が正しく理解していれば、アクセストークンをユーザーエージェントに公開していない明示的認証コード許可フローを使用できます。次の図は、認証コードグラントの流れを示しています enter image description here

そして、ここではあなたが参照するためのOffice 365のリソースに対するアクセストークンを取得するためのコアコードです:

var signInUserId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value; 
     var userObjectId = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value; 

     AuthenticationContext authContext = new AuthenticationContext(SettingsHelper.Authority, new ADALTokenCache(signInUserId)); 

     try 
     { 
      DiscoveryClient discClient = new DiscoveryClient(SettingsHelper.DiscoveryServiceEndpointUri, 
       async() => 
       { 
        var authResult = await authContext.AcquireTokenSilentAsync(SettingsHelper.DiscoveryServiceResourceId, 
                       new ClientCredential(SettingsHelper.ClientId, 
                            SettingsHelper.ClientSecret), 
                       new UserIdentifier(userObjectId, 
                            UserIdentifierType.UniqueId)); 
        string token= authResult.AccessToken; 
        return authResult.AccessToken; 
       }); 

      var dcr = await discClient.DiscoverCapabilityAsync(capabilityName); 

      return new OutlookServicesClient(dcr.ServiceEndpointUri, 
       async() => 
       { 
        var authResult = await authContext.AcquireTokenSilentAsync(dcr.ServiceResourceId, 
                       new ClientCredential(SettingsHelper.ClientId, 
                            SettingsHelper.ClientSecret), 
                       new UserIdentifier(userObjectId, 
                            UserIdentifierType.UniqueId)); 

        return authResult.AccessToken; 
       }); 
     } 

あなたが参照できる完全なコードサンプルherehereは、明示的な認証フローと関連する認証フローの違いについて説明する役に立つリンクです。

+0

ありがとうございました。基本的には、WCFメソッドを呼び出すことができるSharePOintページにJavaScriptを埋め込むことで、これを実現したいと思います。 –

関連する問題