2016-12-14 4 views
0

HTTP POST要求を使用して特定のユーザーOAuth2ベアラトークンを取得しようとしていますが、何も動作していないようです。グラフAPIはユーザーとしてプログラムで認証します

login_url = 'https://login.microsoftonline.com/' 
authorize_endpoint = '{0}{1}{2}'.format(login_url,config.tenant_id,'/oauth2/authorize') 

bodyvals = {'client_id': config.client_id, 
      'client_secret': config.client_secret, 
      'grant_type': 'client_credentials', 
      'resource':config.resource_endpoint} 

return requests.post(authorize_endpoint, data=bodyvals) 

上記のコードは機能しますが、アプリケーションに代わってトークンを生成します。
私はユーザーの資格情報を渡す方法を見つけることができないし、これに関する文書もありません。

答えがPythonやPowershellであるか一般的な説明であれば、私は気にしません。私はAADで正しく行う方法を理解していないようです。 GraphAPIについては

答えて

1

手動で行うこともできます。私の他の回答はhttps://stackoverflow.com/a/40844983/1658906です。

grant_type=passwordを使用し、oauth2/tokenエンドポイントを呼び出す必要があります。

private async Task<string> GetAccessToken() 
{ 
    string tokenEndpointUri = Authority + "oauth2/token"; 

    var content = new FormUrlEncodedContent(new [] 
     { 
      new KeyValuePair<string, string>("grant_type", "password"), 
      new KeyValuePair<string, string>("username", Username), 
      new KeyValuePair<string, string>("password", Password), 
      new KeyValuePair<string, string>("client_id", ClientId), 
      new KeyValuePair<string, string>("client_secret", ClientSecret), 
      new KeyValuePair<string, string>("resource", PowerBiResourceUri) 
     } 
    ); 

    using (var client = new HttpClient()) 
    { 
     HttpResponseMessage res = await client.PostAsync(tokenEndpointUri, content); 

     string json = await res.Content.ReadAsStringAsync(); 

     AzureAdTokenResponse tokenRes = JsonConvert.DeserializeObject<AzureAdTokenResponse>(json); 

     return tokenRes.AccessToken; 
    } 
} 

要求では、指定する必要があります:

  1. ユーザー名
  2. パスワード
  3. 秘密クライアントID
  4. クライアント
  5. リソースURI
  6. ここで認証するためのC#バージョンであります
+0

resource = graph.windows.net? – 4c74356b41

+0

はい、 'https:// graph.windows.net /'です。 – juunas

+0

hm、それよりも複雑に思えますが、レスポンスコードを取得する方法を知っていますか?同様のURLにアクセスしたときに得られるものと同様です。https://login.microsoftonline.com/common/oauth2/v2.0/authorize?response_type=code&client_id=b4675331-58f1-4f5b-9867-e3ea0e76a4d4&redirect_uri=https%3A %2F%2Flocalhost%3A5555%2Flogin%2Fazure%2Fauthorized&scope = user.read&state = 3OCHfg9MJagie0YIoXUsanC6ASJKbk – 4c74356b41

0

、リソースは、あなたがADALを使用しない場合、あなたはしかし、「資源」の利用のためのコードを見てかかることがあります「https://graph.windows.net/

です。このシナリオでは、ADALを大きなサンプルと見なします:)

また、msrestazureには、GraphAPIでも機能するUserPassCredentialsインスタンスがあります。

+0

adalはアプリケーション認証用で、ユーザー認証用ではありませんか? – 4c74356b41

+0

ADALは認証用の汎用ライブラリです。 ADが提供するものは、デバイスコード、ユーザー/パス、サービスプリンシパルなどを使用できます。詳細については、ADALウェブサイトを参照してください。 –

関連する問題