2017-05-09 5 views
0

adal.js을 사용하여 액세스 토큰을 사용하여 https://graph.microsoft.com/v1.0/me 끝점 (또는 graph.windows.net)을 호출 할 때마다 Microsoft OAuth를 통해 액세스 토큰을 생성합니다. 다음 오류가 나타납니다. Authentication_MissingOrMalformed : 액세스 토큰이 없거나 형식이 잘못되었습니다.OAuth를 얻은 후에 Microsoft Graph API에 액세스 할 수 없습니다.

어떻게 해결할 수 있습니까?

<script src="https://secure.aadcdn.microsoftonline-p.com/lib/1.0.12/js/adal.min.js"></script> 
<script> 
    var authContext = new AuthenticationContext({ 
     instance: 'https://login.microsoft.com/',  
     tenant: 'xxxxxx-xxxxxxx-xxxxxx-xxxxxx', //COMMON OR YOUR TENANT ID 
     clientId: 'xxxxxx-xxxxxxx-xxxxxx-xxxxxx', //REPLACE WITH YOUR CLIENT ID 
     redirectUri: '/login.php', //REPLACE WITH YOUR REDIRECT URL 
     callback: getUser, 
     popUp: true, 
     cacheLocation: 'localStorage' 
    }); 

    ... 

    authContext.login(); 
    // SET COOKIE 
    var newToken = authContext.getCachedToken('tenantid-xxxxxxx-xxxxxx-xxxxxx'); 
    var now = new Date(); 
    now.setTime(now.getTime() + 1 * 3600 * 1000); 
    document.cookie = "token="+newToken+"; expires=" + now.toUTCString() + "; path=/"; 
</script> 

그리고 여기에 내가/끌어 내 PHP 스크립트에서 토큰을 사용하려고 해요 방법은 다음과 같습니다 : 여기 내 설정은 JS에서의 Authentication_MissingOrMalformed :

<?php 
// Get the token 
$token = $_COOKIE['token']; 

// Set headers 
$headers = array(
    "Authorization: Bearer " . $token, 
    'Content-Type: application/json' 
); 

// Make request to Graph API 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "https://graph.windows.net/mywebsite.org/me?api-version=1.6"); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_POST, 0); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$response = curl_exec($ch); 
$response = json_decode($response); 
curl_close($ch); 

echo "<pre>"; 
var_dump($response); 
echo "</pre>"; 
?> 

는 않는 모든이 오류를 반환 할 것입니다 액세스 토큰이 누락되었거나 형식이 잘못되었습니다.

어떻게 해결할 수 있습니까 ?? 올바른 자원을 지정하지 않으면 문제가됩니까?

+0

문제가 해결 되었습니까? 안다면 저에게 알려주십시오. –

답변

0

Azure AD Graph REST를 성공적으로 호출하려면 Azure AD Graph에 대한 토큰을 얻어야합니다.

토큰이 Azure AD Graph에 대해 올바른지 확인하려면 토큰을 인쇄하고 here에서 구문 분석 할 수 있습니다.

토큰의 audhttps://graph.windows.net이어야합니다. 일치하지 않으면 캐시에서 토큰을 가져 오는 대신 acquireToken을 사용하여 토큰을 얻어야합니다. resource 매개 변수는 https://graph.windows.net이어야합니다.

+0

Microsoft Graph를 호출하려면'''aud'''와'''resource'' 매개 변수가'''https : // graph.microsoft.com''' 인 경우에 추가하면됩니다. –