我正在尝试使用技能管理API(SMAPI)检索Alexa开发者帐户中的技能列表。
我有以下HTML / javascript:
<BODY>
<a href id="LoginWithAmazon">
<img border="0" alt="Login with Amazon" src="https://images-na.ssl-images-amazon.com/images/G/01/lwa/btnLWA_gry_312x64.png" width="156" height="32" />
</a>
<div id="amazon-root"></div>
<script type="text/javascript">
var client_id = "<client id>";
window.onAmazonLoginReady = function() {
amazon.Login.setClientId(client_id);
};
(function(d) {
var a = d.createElement('script'); a.type = 'text/javascript';
a.async = true; a.id = 'amazon-login-sdk';
a.src = 'https://api-cdn.amazon.com/sdk/login1.js';
d.getElementById('amazon-root').appendChild(a);
})(document);
document.getElementById('LoginWithAmazon').onclick = function() {
options = {
scope : 'profile postal_code alexa::ask:skills:readwrite alexa::ask:models:readwrite alexa::ask:skills:test',
interactive: 'always',
response_type: 'code'
};
amazon.Login.authorize(options, '<login page url>');
return false;
};
</script>
</BODY>
然后调用登录页面以获取适当的访问令牌:
$grant_type = 'authorization_code';
$client_id = $GLOBALS['client_id']; //your client id
$client_secret = $GLOBALS['client_secret']; //your client secret
$data = array(
"grant_type"=>"authorization_code",
"code" => $code,
"client_id"=> $client_id,
"client_secret"=> $client_secret
);
$postvars = '';
foreach($data as $key=>$value) {
$postvars .= $key . "=" . $value . "&";
}
$ch = curl_init('https://api.amazon.com/auth/o2/token');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded;charset=UTF-8'));
$result = curl_exec($ch);
curl_close($ch);
$result_arr = json_decode($result, true);
if (isset($result_arr['error']) == true){ //there was an error obtaining the auth token
var_dump($result);
die('Error: There was an error authenticating with Amazon. Can you please start over again? Click <a href="index.php">here</a> to start again.');
}
return $result_arr;
我可以使用$ result_arr中的access_token来获取个人资料信息,但是当我用它来获取技能列表时:
// exchange the access token for list of skills
$c = curl_init('https://api.amazonalexa.com/v0/skills/');
curl_setopt($c, CURLOPT_HTTPHEADER, array('Authorization: ' . $access_token));
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_VERBOSE, 1);
curl_setopt($c, CURLOPT_POST, 1);
$r = curl_exec($c);
curl_close($c);
var_dump($r);
我收到用户未同意此操作
我必须遗漏一些基本的东西。我的印象是初始请求中的范围:profile postal_code alexa::ask:skills:readwrite alexa::ask:models:readwrite alexa::ask:skills:test
足以提供访问权限。我已确认亚马逊帐户显示该应用可以访问上述权限。