2017-02-09 76 views
0

我開發laravel API和UI是angularjs,問題是在頁面獲取CORS問題重定向到微軟標誌的微軟圖形時,這個問題是頁面獲取CORS問題重定向時微軟標誌的微軟圖形

XMLHttpRequest無法加載 https://reservations-api.nymblpro.com/coordinator/event。從 'https://reservations-api.nymblpro.com/coordinator/event' 重定向到 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize?state=cuSlE1 ... rect_uri = HTTPS%3A%2F%2Freservations-api.nymblpro.com%2Fcoordinator%2Fevent' 已被阻止通過CORS政策:請求需要預檢,這是不允許的 跟隨橫 - 原始重定向。

我試着設置cors中間件仍然是我得到同樣的問題。 我的代碼如下:

public function get_microsoft_token(Request $request) 
{ 
    $provider = new \League\OAuth2\Client\Provider\GenericProvider([ 
     'clientId'    => 'fd482697-fd9f-46ac-ab3a-727e47517c8b', 
     'clientSecret'   => 'WZOh3qLz7ZQyKemCQf3RsCF', 
     'redirectUri'    => 'https://reservations-api.nymblpro.com/coordinator/event', 
     'urlAuthorize'   => 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize', 
     'urlAccessToken'   => 'https://login.microsoftonline.com/common/oauth2/v2.0/token', 
     'urlResourceOwnerDetails' => '', 
     'scopes'     => 'openid calendars.readwrite' 
    ]); 
    if (!$request->has('code')) { 
     return redirect($provider->getAuthorizationUrl()); 
    } 
    else { 
     $accessToken = $provider->getAccessToken('authorization_code', [ 
      'code' => $request->input('code') 
     ]); 
     return ($accessToken->getToken()); 
    } 
} 
+0

http://stackoverflow.com/questions/40388152/microsoft-graph-download-file-content-returns-404/40400293#40400293和http://stackoverflow.com/questions/34949492/cors-request-with -preflight-and-redirect-disallowed-workarounds/39728229#39728229可能與此有關 – sideshowbarker

+0

如果您還發布了發送請求的客戶端JavaScript代碼的相關部分,這將會很有幫助。 – sideshowbarker

+0

@sideshowbarker這裏是JavaScript'$ scope.getTokenForCalender = function() { var url =「https://reservation-api.dev/coordinator/event」; (函數(結果){ console.log(「Token For Calender Integration - 」+ JSON.stringify(result)); },function(err) console.log(JSON.stringify(err)); alert(「Error Occured ....」); }); } $ scope.getTokenForCalender(); 這裏err爲空 –

回答

0

它看起來像你的AngularJs應用程序將調用您的Laravel應用程序的API,它返回授權的URL,那麼你的angularJs前端將調用的網址,AJAX功能。所以這應該是罪犯。

您可以嘗試修改代碼在你angularjs前端應用,改變類似$http.get()功能是這樣的:

// similar behavior as an HTTP redirect 
window.location.replace("http://stackoverflow.com"); 

// similar behavior as clicking on a link 
window.location.href = "http://stackoverflow.com"; 

否則,你可以嘗試使用阿達爾的JS在你angularjs應用。有關更多信息,請參閱https://github.com/AzureAD/azure-activedirectory-library-for-js

+0

我能夠通過直接重定向到授權簽名頁面繞過此問題,謝謝@garry liu和sidesshowbarker –