2017-11-08 14 views
0

現在、https://login.microsoftonline.com/XXX/oauth2/tokenエンドポイントへポストリクエストを送信して、アプリケーションのアクセストークンとリフレッシュトークンを取得しようとしています。 axiosを使用してポスト要求をエンドポイントに送信すると、プリフライトはオフになりますが、応答は返されません。Azure ADプリフライトリクエストがデータを返さない

エラー:両方Axios要求を

No 'Access-Control-Allow-Origin' header is present on the requested resource. 
Origin 'http://localhost:3000' is therefore not allowed access. 

Response to preflight request doesn't pass access control check: 
No 'Access-Control-Allow-Origin' header is present on the requested resource. 
Origin 'http://localhost:3000' is therefore not allowed access. 

しかしaxiosポスト要求に異なるアプローチを使用して、それがデータを返しますが、何のプリフライトを持っていないし、別のエラーを与えます:

const data = new FormData(); 

data.append('grant_type', this.config.grant_type); 
data.append('client_id', this.config.client_id); 
data.append('code', localStorage.getItem('auth_code')); 
data.append('redirect_uri', this.config.redirect_uri); 
data.append('client_secret', this.config.client_secret); 
data.append('resource', this.config.client_id); 

axios.post(`https://login.microsoftonline.com/${this.config.tenant}/oauth2/token`, data); 

方法2:

axios({ 
    method: 'post', 
    contentType: 'application/json', 
    url: `https://login.microsoftonline.com/${this.config.tenant}/oauth2/token`, 
    data: { 
    grant_type: this.config.grant_type, 
    client_id: this.config.client_id, 
    code: localStorage.getItem('auth_code'), 
    redirect_uri: this.config.redirect_uri, 
    client_secret: this.config.client_secret, 
    resource: this.config.client_id 
    } 
}); 

これはaxios要求自体またはエンドポイントに問題がありますか?

答えて

0

アクセストークンを取得するには、暗黙的な許可フローを使用する必要があります。フロントエンドJavaScriptから認証コードフローを使用することはできません!

Your client secret (AKA your app's password) is currently public to anyone who visits your site!

あなたは、フロントエンドJavaScriptでクライアントシークレットを使用することはできません。

あなたはアプリのマニフェストで暗黙の流れを有効にする必要があり、その後、あなたのアプリケーションでは、このようなURLでのAzure ADにリダイレクトを行います:

https://login.microsoftonline.com/tenant-id-here/oauth2/authorize?client_id=your-client-id&response_type=id_token+token&resource=resource-id-for-api&redirect_uri=your-app-redirect-url 

ドキュメント:https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-authentication-scenarios#single-page-application-spa

関連する問題