{“消息”:“用户不同意此操作。”} HTTP / 1.1 401未经授权

时间:2019-03-26 09:35:35

标签: java dialogflow alexa

我通过client_credential授权类型使用client_id和client secret获取访问令牌,但是我仍然无法使用我使用client_id和client_secret(在Amazon安全配置文件上创建)的Alexa技能访问资源(帐户链接详细信息)。

这是我获取访问令牌的代码:

HttpPost httpPost = new HttpPost("https://api.amazon.com/auth/o2/token");
    HttpClient client = new DefaultHttpClient();// HttpClientBuilder.create().build();
    httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(
            new BasicNameValuePair("client_id", "amzn1.appl***************************************98"));
    params.add(new BasicNameValuePair("client_secret",
            "1e5d*****************************************eb2"));
    params.add(new BasicNameValuePair("grant_type", "client_credentials"));
    params.add(new BasicNameValuePair("scope", "adx_reporting::appstore:marketer"));
    try {
        httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    try {
        HttpResponse response = client.execute(httpPost);
        HttpEntity respEntity = response.getEntity();

        if (respEntity != null) {
            String content = EntityUtils.toString(respEntity);
            System.out.println(content);
            System.out.println(response.getStatusLine());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

这是我得到信息的代码:

  

{“消息”:“用户不同意此操作。”}   HTTP / 1.1 401未经授权

我的代码:

String stage = "development";
    String skillId = "amz*************************************dd";
    String url = "https://api.amazonalexa.com/v1/skills/" + skillId + "/stages/" + stage
            + "/accountLinkingClient";
    HttpClient client = new DefaultHttpClient();
    HttpGet get = new HttpGet(url);
    get.addHeader("Authorization", "Bearer " + token);
    try {
        HttpResponse response = client.execute(get);
        HttpEntity respEntity = response.getEntity();

        if (respEntity != null) {
            String content = EntityUtils.toString(respEntity);
            System.out.println(content);
            System.out.println(response.getStatusLine());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

0 个答案:

没有答案