我已开始从实时导入联系人。现在我不知道微软在想什么,但他们确实把他们所做的一切都变得过于复杂了。
对于我的应用程序来说,获取电话号码非常重要。事实上,非常重要的是,如果您没有电话号码,您的联系方式将被跳过。用我的方法我看不到任何电话号码。我以为如果我一一循环每个联系人就会显示出来,但是可惜,没有爱。
这是我的方法:
$import_id = time();
$client_id = "xxx";
$redirect_uri = 'redirecturi';
$client_secret = "xxx";
$code = $_GET['code'];
$grant_type = "authorization_code";
$post = "client_id=$client_id&redirect_uri=$redirect_uri&client_secret=$client_secret&code=$code&grant_type=$grant_type";
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,"https://login.live.com/oauth20_token.srf");
curl_setopt($curl,CURLOPT_POST,5);
curl_setopt($curl,CURLOPT_POSTFIELDS,$post);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
$result = curl_exec($curl);
curl_close($curl);
$token = json_decode($result);
$access_token = $token->access_token;
$user_id = $token->user_id;
$url = "https://apis.live.net/v5.0/me/contacts?access_token=$access_token";
$response = curl_file_get_contents($url);
$response = json_decode($response);
foreach($response->data as $contact) {
$contact_details = curl_file_get_contents("https://apis.live.net/v5.0/" . $contact->id . "?access_token=$access_token");
debug($contact_details);
}
die();
但是,我只能收到这样的信息(我认识的这个人有一个联系电话,当我在 people.live.com 上查看他时可以看到):
{
"id": "contact.id",
"first_name": "Danie",
"last_name": "Van den Heever",
"name": "Danie Van den Heever",
"is_friend": false,
"is_favorite": false,
"user_id": "userid",
"email_hashes": [
"emailhash"
],
"updated_time": "2014-09-17T12:11:10+0000"
}
我的权限请求 URL(定义范围)如下所示:
https://login.live.com/oauth20_authorize.srf?client_id=clientidkey&scope=wl.basic%20wl.offline_access&response_type=code&redirect_uri=redirecturi
我应该添加更多范围来获取联系号码吗?如果有,范围是哪些?或者这不可能吗?
最佳答案
解决方案是使用未记录的范围 wl.contacts_phone_numbers
,存在被弃用或被锁定的风险,并且只有 Microsoft 批准的客户端才能使用它,但是同时它起作用了。
此外,您不需要对每个联系人执行额外的请求,您从 me/contacts
获取的联系人对象已在 phones
对象中包含电话号码。
顺便说一句,这是我在测试时使用的代码,我使用了 REST client library这样可以避免每次都复制/粘贴冗长且重复的 cURL 参数,并将请求变成单行代码。
请求许可的代码:
$params = ["client_id" => "...", "scope" => "wl.basic wl.contacts_phone_numbers", "response_type" => "code", "redirect_uri" => "http://sanctuary/contacts_callback.php"];
header("Location: https://login.live.com/oauth20_authorize.srf?".http_build_query($params));
请注意权限请求中额外的 wl.contacts_phone_numbers
范围。
获取访问 token 并检索联系人的代码:
// Composer's autoloader, required to load libraries installed with it
// in this case it's the REST client
require "vendor/autoload.php";
// exchange the temporary token for a reusable access token
$resp = GuzzleHttp\post("https://login.live.com/oauth20_token.srf", ["body" => ["client_id" => "...", "client_secret" => "...", "code" => $_GET["code"], "redirect_uri" => "http://sanctuary/contacts_callback.php", "grant_type" => "authorization_code"]])->json();
$token = $resp["access_token"];
// REST client object that will send the access token by default
// avoids writing the absolute URL and the token each time
$client = new GuzzleHttp\Client(["base_url" => "https://apis.live.net/v5.0/", "defaults" => ["query" => ["access_token" => $token]]]);
// get all the user's contacts
$contacts = $client->get("me/contacts")->json()["data"];
// iterate over contacts
foreach ($contacts as $contact) {
// if that contact has a phone number object
if (array_key_exists("phones", $contact)) {
// iterate over each phone number
foreach ($contact["phones"] as $phone) {
// if number isn't blank
if (!empty($phone)) {
// do whatever you want with that number
}
}
}
}
这是带有额外范围的 me/contacts
的样子(减去一些换行符和个人信息):
Array (
[data] => Array (
[0] => Array (
[id] => contact...
[first_name] => ...
[last_name] => ...
[name] => ...
[is_friend] =>
[is_favorite] =>
[user_id] =>
[email_hashes] => ...
[updated_time] => ...
[phones] => Array ( // what you asked for
[personal] =>
[business] =>
[mobile] => +337...
)
)
)
)
关于php - 导入 Windows Live 联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25908398/