【问题标题】:Bing Search API Azure Marketplace Authentication in JavaJava 中的必应搜索 API Azure 市场身份验证
【发布时间】:2012-06-23 14:46:21
【问题描述】:

如何在 Java 中进行身份验证以使用 Azure Marketplace 中的新必应搜索 API?迁移指南未提供有关 Java 的信息

【问题讨论】:

    标签: authentication search azure odata bing-api


    【解决方案1】:

    您需要将您的 accountKey 编码为 Base64 并使用 Authorization 标头将其传递给每个请求。

    String bingUrl = "https://api.datamarket.azure.com/Bing/Search/................";
    
    String accountKey = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
    byte[] accountKeyBytes = Base64.encodeBase64((accountKey + ":" + accountKey).getBytes());
    String accountKeyEnc = new String(accountKeyBytes);
    
    URL url = new URL(bingUrl);
    URLConnection urlConnection = url.openConnection();
    urlConnection.setRequestProperty("Authorization", "Basic " + accountKeyEnc);
    
    ...
    

    此代码基于Migrating to the Bing Search API in Windows Azure Marketplace 文档中的 PHP 示例。

    更新:修改encodeBase64调用,应该是这样的:accountKey + ":" + accountKey

    【讨论】:

    • 方法似乎是正确的,但 setRequestProperty 中的某些内容必须不同,因为这样我会收到响应消息“需要基本身份验证。输入帐户密钥作为密码。
    • 您的新代码会产生“错误请求”。我也试过 httpsCon.setRequestProperty("Authorization: Basic",accountKeyEnc);但我再次收到消息“需要基本身份验证...”@sandrinodimatia
    • 如果您收到错误请求,我假设身份验证正常,但实际请求有问题。您使用的完整 url 是什么(例如,您是否处理了 url 编码)?你要发送什么数据?
    • 我打开连接的网址是“api.datamarket.azure.com/Bing/Search/…”。我打开的连接是一个名为 httpsCon 的 HttpsURLConnection。在 httpsCon.setRequestProperty(....) 行之后,我只是尝试通过生成代码 400 的方法 getResponseCode 和生成字符串“Bad Request”的 getResponceMessage 来查看它是否连接良好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-22
    • 2016-12-21
    • 2017-02-04
    • 2013-06-28
    • 1970-01-01
    • 2016-07-26
    • 2012-08-02
    相关资源
    最近更新 更多