2015-09-28 81 views
2

我想從當前用戶通過Oauth2 Microsoft Online連接所有的聯繫人,我做了Oauth2的事情,但當我打電話給api的url http://outlook.office.com/api/v1.0/me/contacts我得到了一個空字符串和這在標頭:從Outlook導入聯繫人api訪問禁止

HTTP/1.1 403 Forbidden 
Cache-Control: private 
Content-Length: 0 
Server: Microsoft-IIS/8.5 
Set-Cookie: ClientId=QPAIYPSBRKGUWYWEQFBAA; expires=Tue, 27-Sep-2016 12:07:04 GMT; path=/; secure; HttpOnly 
request-id: d24bcf29-372e-4bbc-bb7c-f753cde1d1dc 
X-CasErrorCode: UserNotFound 
X-AspNet-Version: 4.0.30319 
X-Powered-By: ASP.NET 
X-FEServer: AMSPR03CA0038 

沒有別的。

這裏是我的代碼在PHP與家居的OAuth。代碼使用Laravel :)

public function outlook(Request $request) 
{ 
    if($request->session()->has('tokenOutlook')) 
    { 
     return redirect('/contact/outlook/all'); 
    } 
    else 
    { 
     if(!$request->has('code')) 
     { 
      return redirect("https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=".env('APPIDOUTLOOK')."&redirect_uri=".secure_url('/contact/outlook')."&response_type=code&scope=".urlencode("https://outlook.office.com/Contacts.Read")); 
     } 
     else 
     { 
      $curl = curl_init("https://login.microsoftonline.com/common/oauth2/v2.0/token"); 
      curl_setopt($curl, CURLOPT_POST, true); 
      $postdata = array(
       "client_id" => env('APPIDOUTLOOK'), 
       "code" => $request->get('code'), 
       "grant_type"=>"authorization_code", 
       "scope" => urlencode("https://outlook.office.com/Contacts.Read"), 
       "client_secret" => env('APPSECRETOUTLOOK') 
      ); 
      $fields_string = ''; 
      foreach($postdata as $key=>$value) { $fields_string .= $key.'='.$value.'&'; } 
      curl_setopt($curl, CURLOPT_POSTFIELDS, $fields_string); 

      curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 

      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
      curl_setopt($curl, CURLOPT_TIMEOUT, 10); 
      $oauth_token = curl_exec($curl); 
      curl_close($curl); 
      $oauth_token = json_decode($oauth_token); 
      //dd($oauth_token) 
      $request->session()->put('tokenOutlook', $oauth_token->access_token); 
      return redirect('/contact/outlook/all'); 
     } 
    } 
} 
public function showOutlook(Request $request) 
{ 
    if($request->session()->has('tokenOutlook')) 
    { 
     //dd($request->session()->get('tokenOutlook')); 
     $curl = curl_init('https://outlook.office.com/api/v1.0/me/contacts'); 
     curl_setopt($curl, CURLOPT_HEADER, true); 
     curl_setopt($curl, CURLOPT_VERBOSE, true); 

     curl_setopt($curl, CURLOPT_HTTPHEADER, array("Authorization: Bearer ".$request->session()->get('tokenOutlook'))); 
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 

     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
     curl_setopt($curl, CURLOPT_TIMEOUT, 10); 
     $contacts = curl_exec($curl); 
     $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE); 
     $header = substr($contacts, 0, $header_size); 
     //var_dump($header); 
     //dd($curl); 
     curl_close($curl); 
     $contacts = json_decode($contacts); 
     $array_contacts = []; 

     return $array_contacts; 
    } 
    else 
    { 
     return redirect('/contact/outlook'); 
    } 
} 

爲什麼我得到一個UserNotFound錯誤/我? :(

+0

確保用戶是要oauthplay.azurewebsites.net確定。嘗試登錄和做聯繫人API調用。 –

+0

沙箱正常工作與我,但我加入了「REDIRECT_URI」解決我的問題=> SECURE_URL(「/聯繫人/前景展望」)在POSTDATA ...... – Roadirsh

+0

啊,丟失的重定向。你應該把那個作爲回答:) –

回答

0

我已經通過增加後的數據REDIRECT_URI解決我的問題......

$postdata = array(
      "client_id" => env('APPIDOUTLOOK'), 
      "code" => $request->get('code'), 
      "grant_type"=>"authorization_code", 
      "scope" => urlencode("https://outlook.office.com/Contacts.Read"), 
      "client_secret" => env('APPSECRETOUTLOOK') 
      "redirect_uri" => secure_url('/contact/outlook') 
     );