从 Outlook 导入联系人 api 禁止访问
Import contact from Outlook api access forbidden
我正在尝试通过 Oauth2 Microsoft Online 获取当前用户的所有联系人,我正确地执行了 Oauth2 操作,但是当我为 api [=] 调用 url 时13=] 我得到一个空字符串,这个在 Header:
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');
}
}
为什么我使用 /me 时会出现 UserNotFound 错误? :(
我通过在 post 数据中添加 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')
);
我正在尝试通过 Oauth2 Microsoft Online 获取当前用户的所有联系人,我正确地执行了 Oauth2 操作,但是当我为 api [=] 调用 url 时13=] 我得到一个空字符串,这个在 Header:
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');
}
}
为什么我使用 /me 时会出现 UserNotFound 错误? :(
我通过在 post 数据中添加 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')
);