导入Windows Live联系人 [英] Importing Windows Live Contacts
问题描述
我从实时导入联系人开始.现在我不知道MS在想什么,但是他们严重地使他们要处理的一切变得过于复杂.
I've started with importing contacts from live. Now I don't know what MS is thinking, but they seriously do overcomplicate everything they put their hands to.
对于我的应用程序,获得电话号码非常重要.实际上非常重要,因此如果您没有电话号码,则会跳过您的联系人.用我的方法,我看不到任何电话号码.我以为如果我一个接一个地遍历每个联系人就会显示出来,但是可惜,没有爱.
For my app, it's very important that I get a phone number. So important in fact, that should you not have a phone number, your contact is skipped. With my method I can't see any phone numbers. I assumed that it would be shown if I loop through each contact one by one, but alas, no love.
这是我的方法:
$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上查看他时可以看到它):
However, I'm only getting info back like this (this person I know has a contact number as I can see it when I view him on 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"
}
我的权限请求网址(定义了范围)如下所示:
My permission request url (which defines the scopes) looks like this:
https://login.live.com/oauth20_authorize.srf?client_id=clientidkey&scope=wl.basic%20wl.offline_access&response_type=code&redirect_uri=redirecturi
我应该添加更多范围以获取联系电话吗?如果是这样,范围是什么?还是不可能?
Should I add more scopes to get the contact number? If so, which scopes? Or is this not possible?
推荐答案
解决方案是使用未记录的作用域wl.contacts_phone_numbers
,存在被弃用或锁定的风险,只有Microsoft批准的客户端才能使用可以使用它,但与此同时它可以工作.
The solution is to use an undocumented scope wl.contacts_phone_numbers
, there is a risk that it'll become deprecated or just locked down and only Microsoft-approved clients will be able to use it, but in the meantime it works.
此外,您无需为每个联系人进行额外的请求,从me/contacts
获取的联系人对象已经在phones
对象中包含电话号码.
Also you do not need to do an extra request for every contact, the contact object you get from me/contacts
already has the phone numbers in a phones
object.
顺便说一下,这是我在测试此代码时使用的代码,我使用了 REST客户端库避免了每次都复制/粘贴冗长且重复的cURL参数,并将请求变成单行.
By the way, here's the code I used while testing this, I used a REST client library which avoids copy/pasting the long and repetitive cURL parameters each time and turns the requests into one-liners.
要求许可的代码:
$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
范围.
Note the extra wl.contacts_phone_numbers
scope in the permission request.
获取访问令牌并检索联系人的代码:
Code to get access token and retrieve contacts :
// 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
的范围如下(减去一些换行符和个人信息):
Here's what me/contacts
looks like with the extra scope (minus a few line breaks and personal info) :
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...
)
)
)
)
这篇关于导入Windows Live联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!