2015-11-03 111 views
0

我花了好几天的时间试图弄清楚这一点,看看我可以在Azure网站,Azure git集线器以及所有相关的堆栈溢出帖子中找到的所有信息。我希望我缺少一些简单的东西。Azure Billing API返回403

我使用的示例Java代码被发布在网络上得到的令牌:

try { 
     exec = Executors.newFixedThreadPool(1); 
     context = new AuthenticationContext("https://login.microsoftonline.com/8e4f0713-5eea-4da0-99c0-xxxxxxxxxxxx", 
         true, exec); 
     ClientCredential cred = new ClientCredential(webClientID, clientSecret); 
     Future<AuthenticationResult> future = context.acquireToken("https://management.azure.com/", cred, null); 
     result = future.get(); 
    } catch(Exception e) { 
     logger.warn("Exception " + e); 
    } finally { 
     exec.shutdown(); 
    } 

    if (result == null) { 
     return null; 
    } 
    return result.getAccessToken(); 

这产生一个令牌,其余放入请求头:

Authorization: Bearer -token- 

The GET https://management.azure.com/subscriptions/758ad253-cbf5-4b18-8863-xxxxxxxxxxxx/providers/Microsoft.Commerce/RateCard?api-version=2015-06-01-preview%26%24filter%3DOfferDurableId+eq+%27MS-AZR-0003p%27+and+Currency+eq+%27USD%27+and+Locale+eq+%27en-US%27+and+RegionInfo+eq+%27US%27 

返回403码:

Exception: java.io.IOException: Server returned HTTP response code: 403 for URL: https://management.azure.com/subscriptions/758ad253-cbf5-4b18-8863-xxxxxxxxxxxx/providers/Microsoft.Commerce/RateCard?api-version=2015-06-01-preview%26%24filter%3DOfferDurableId+eq+%27MS-AZR-0003p%27+and+Currency+eq+%27USD%27+and+Locale+eq+%27en-US%27+and+RegionInfo+eq+%27US%27 

接头: [0]空:HTTP/1.1 403禁止

[1]的Cache-Control:无缓存

[2]的Pragma:无缓存

[3]内容类型:应用/ JSON;字符集= UTF-8

[4]过期:-1

[5] X-MS-失败原因:网关

[6] X-MS-请求-ID:e4ad9253-E034 -481d-aba0-f46902b7057f

[7] X-MS-相关请求-ID:e4ad9253-e034-481d-aba0-f46902b7057f

[8] X-MS-路由请求-ID:EASTUS :20151103T205103Z:e4ad9253-e034-481d-aba0-f46902b7057f

[9]严格交通安全:最大年龄= 31536000; includeSubDomains

[10]日期:星期二,2015年11月3日20时51分02秒GMT

[11]连接:关闭

[12]的Content-Length:303

我所做的一切在Azure MGMT控制台上的设置,以创建应用程序在Azure的AD,得到clientID的&客户端密钥等。SSL/HTTPS代码:

  azureURL = new java.net.URL(url); 

     con = (HttpsURLConnection)azureURL.openConnection(); 
     con.disconnect(); 
     con.setDoOutput(true); 
     con.setDoInput(true); 
     con.setUseCaches(false); 
     con.setSSLSocketFactory(MyUtils.getSSLSocketFactory()); 
     con.setRequestMethod("GET"); 
     con.setRequestProperty("x-ms-version", "2015-06-01-preview"); 
     con.setRequestProperty("Content-Type", "application/json"); 

     String token = getAccessTokenFromServicePrincipalCredentials(); 
     if (token != null) { 
      con.setRequestProperty("Authorization", "Bearer " + token); 
      con.connect(); 
      in = (InputStream)con.getContent(); 
      InputStreamReader inr = new InputStreamReader(in); 
     } else { 
      logger.warn("unable to obtain prices"); 
     } 

任何建议关于如何调试问题?

回答

0

我看到您获得Service Principal(即应用程序本身)的访问令牌。请确保您在Azure订阅上授予此服务主体用户至少Reader角色。我相信你会得到这个错误,因为这个用户没有任何访问你的Azure订阅的权限。

有关如何分配角色,请参阅此链接:https://azure.microsoft.com/en-in/documentation/articles/role-based-access-control-configure/Manage access using the Azure Management Portal部分)。

+0

谢谢,我添加了应用程序本身作为贡献者,403消失了。现在我得到一个404文件未找到。 –

+0

您应该在错误中获得更多详细信息。你介意分享这些细节吗? –

+0

这是一个例外:java.io.FileNotFoundException:https://management.azure.com/subscriptions/758ad253-cbf5-4b18-8863-3eed0825bf07/providers/Microsoft.Commerce/RateCard?api-version=2015-06- 01预览%26%24filter%3DOfferDurableId + EQ +%27MS-AZR-0003p%27 +和+货币+ EQ +%27USD%,27 +和+地点+ EQ +%27en美%27 +和+ RegionInfo + EQ +%27US% 27 –

0

因为我们不能拨打电话使用情况&通过HTTPS请求证书评估卡API。正如提到:

所有您在使用Azure的资源管理器的资源完成的任务必须与Azure的Active Directory的

Authenticating Azure Resource Manager requests进行身份验证。

所以你得到了403问题。

请尝试使用在Resource Usage (Preview)上提及的请求头Content-Type和Authorization来为REST API构建公共HTTP请求。你

还可以测试得到的HTTP请求生成工具,你需要的信息,如:

enter image description here

这里是供您参考类似线程How to use Management certificate based authentication for making REST API calls to Azure?

0

对于没有发现回复该文件是:

java.io.FileNotFoundException:

https://management.azure.com/subscriptions/758ad253-cbf5-4b18-8863-3eed082xxxxx/providers/Microsoft.Commerce/RateCard?api-version=2015-06-01-preview%26%24filter%3DOfferDurableId+eq+%27MS-AZR-0003p%27+and+Currency+eq+%27USD%27+and+Locale+eq+%27en-US%27+and+RegionInfo+eq+%27US%27

HTTP/1.1 404未找到

[1]的Cache-Control:无-cache

[2] Pragma:no-cache

[3] Content-Type:application/json;字符集= UTF-8

[4]过期:-1

[5] X-MS-失败原因:网关

[6] X-MS-请求-ID:8bd5ea3a-5a5f -4eb5-86b5-bd6581f94e00

[7] X-MS-相关请求-ID:8bd5ea3a-5a5f-4eb5-86b5-bd6581f94e00

[8] X-MS-路由请求-ID:EASTUS :20151119T181954Z:8bd5ea3a-5a5f-4eb5-86b5-bd6581f94e00

[9]严格交通安全:最大年龄= 31536000; includeSubDomains

[10]日期:星期四,2015年11月19日18点19分53秒GMT

[11]的Content-Length:348

让我看看还有什么我能得到...