Watson Visual Recognition - 未经授权:由于凭据无效,访问被拒绝

Watson Visual Recognition - Unauthorized: Access is denied due to invalid credentials

我按照重新预订 (http://www.redbooks.ibm.com/redbooks/pdfs/sg248393.pdf) 中的步骤启动了一个小型 java 应用程序。 我还创建了一个 API 密钥,就像他们描述的那样。

现在我收到一条未经授权:由于凭据无效,访问被拒绝。

package vrproject;

//Here you import Watson Java SDK to make it available in your code. 
import com.ibm.watson.developer_cloud.visual_recognition.v3.*;
import com.ibm.watson.developer_cloud.visual_recognition.v3.model.*;

public class ClassifyImage {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    VisualRecognition service = new VisualRecognition(VisualRecognition.VERSION_DATE_2016_05_20); 
    service.setEndPoint("https://gateway.watsonplatform.net/visual-recognition/api");

    //Here you replace "your_api_key_here" by the API Key you created in "Creating 
    //a Watson Visual Recognition service instance and getting the API key"
    service.setApiKey("123apikey");

    //Here you add the URL of your image. The image size should not exceed 2MB.
    String imageURL = new
    String("https://raw.githubusercontent.com/watson-developer-cloud/doc-tutorial-downloads/master/visual-recog nition/fruitbowl.jpg");
    ClassifyImagesOptions options = new ClassifyImagesOptions.Builder().url(imageURL).build(); 
    VisualClassification result = service.classify(options).execute(); System.out.println("Classification Results:");
    System.out.println(result);
}

}

错误信息:

    Mär 05, 2020 7:02:23 PM com.ibm.watson.developer_cloud.service.WatsonService processServiceCall
SCHWERWIEGEND: POST https://gateway.watsonplatform.net/visual-recognition/api/v3/classify?version=2016-05-20&api_key=123apikey, status: 401, error: Unauthorized
Exception in thread "main" com.ibm.watson.developer_cloud.service.exception.UnauthorizedException: Unauthorized: Access is denied due to invalid credentials
    at com.ibm.watson.developer_cloud.service.WatsonService.processServiceCall(WatsonService.java:418)
    at com.ibm.watson.developer_cloud.service.WatsonService.execute(WatsonService.java:180)
    at vrproject.ClassifyImage.main(ClassifyImage.java:22)

你知道我为什么会收到这些错误吗? 不知道是不是版本太旧了,我就用书上的那个。

您没有使用正确的服务端点。您链接到的文档显示端点 URL:https://gateway-a.watsonplatform.net/visual-recognition/api

您正在使用 https://gateway.watsonplatform.net/visual-recognition/api

您可以在 Watson Visual Recognition 服务页面获取 API_KEYEndpoint

在视觉识别服务 管理 页面的凭据下,您应该会看到

  • API键
  • URL

URL 格式将是这样的

https://api.REGION.visual-recognition.watson.cloud.ibm.com/instances/INSTANCEID

红皮书发布于2017年,其认证机制现已过时,其使用的SDK已被弃用。您需要根据当前 API 文档修改您的代码:https://cloud.ibm.com/apidocs/visual-recognition/visual-recognition-v3?code=java#classify-images

编写示例代码时的位置

IamAuthenticator authenticator = new IamAuthenticator("{apikey}");
VisualRecognition visualRecognition = new VisualRecognition("2018-03-19", authenticator);
visualRecognition.setServiceUrl("{url}");

InputStream imagesStream = new FileInputStream("./fruitbowl.jpg");
ClassifyOptions classifyOptions = new ClassifyOptions.Builder()
  .imagesFile(imagesStream)
  .classifierIds(Arrays.asList("food"))
  .build();
ClassifiedImages result = visualRecognition.classify(classifyOptions).execute().getResult();
System.out.println(result);x

您的 SDK 依赖项将用于

<dependency>
  <groupId>com.ibm.watson</groupId>
  <artifactId>ibm-watson</artifactId>
  <version>8.3.0</version>
</dependency>