gpt4 book ai didi

java - IBM Watson APIs Java SDK 使用 Watson token 认证失败

转载 作者:搜寻专家 更新时间:2023-11-01 02:36:21 24 4
gpt4 key购买 nike

我使用 Watson APIs Java SDK我的身份验证是使用函数 setUsernameAndPassword(username, password),但现在我想使用 Tokens for authentication .

我的“用户名和密码”代码

mAssistant = new Assistant("2018-02-16");
mAssistant.setUsernameAndPassword(mUserName, mPassword);
InputData input = new InputData.Builder("Hello").build();
MessageOptions options = new MessageOptions.Builder("{workspaceId}")
.input(input)
.build();
MessageResponse response = mAssistant.message(options).execute();
System.out.println(response);

它工作正常。

我使用此方法获取我的 token 。
curl -X GET --user "{username}:{password}""https://gateway.watsonplatform.net/authorization/api/v1/token?url=https://gateway.watsonplatform.net/conversation/api “

身份验证 token
方法一

mAssistant = new Assistant("2018-02-16");
Map<String, String> map = new HashMap<>();
map.put("X-Watson-Authorization-Token", "{token_string}");
mAssistant.setDefaultHeaders(map);
mAssistant.setSkipAuthentication(true);
InputData input = new InputData.Builder("Hello").build();
MessageOptions options = new MessageOptions.Builder("{workspaceId}")
.input(input)
.build();
MessageResponse response = mAssistant.message(options).execute();
System.out.println(response);

获取错误码

E/WatsonService: POST status: 403, error: Forbidden
com.ibm.watson.developer_cloud.service.exception.ForbiddenException: Forbidden
05-07 16:05:57.720 10392-10476/mvi.rcu W/System.err: at com.ibm.watson.developer_cloud.service.WatsonService.processServiceCall(WatsonService.java:401)
05-07 16:05:57.720 10392-10476/mvi.rcu W/System.err: at com.ibm.watson.developer_cloud.service.WatsonService$WatsonServiceCall.execute(WatsonService.java:459)

方法二

mAssistant = new Assistant("2018-02-16");
IamOptions options = new IamOptions.Builder()
.accessToken(token)
.build();
mAssistant.setIamCredentials(options);
mAssistant.setEndPoint("https://gateway.watsonplatform.net/conversation/api");
// do same ...
// mAssistant.message(options).execute();
// ...

获取错误信息

W/System.err: com.ibm.watson.developer_cloud.service.exception.UnauthorizedException: Unauthorized: Access is denied due to invalid credentials. Tip: Did you set the Endpoint?
W/System.err: at com.ibm.watson.developer_cloud.service.WatsonService.processServiceCall(WatsonService.java:398)
W/System.err: at com.ibm.watson.developer_cloud.service.WatsonService$WatsonServiceCall.execute(WatsonService.java:459)

如果我想通过 Watson APIs Java SDK 使用 token 进行身份验证,我该怎么办?

最佳答案

编辑:以下答案适用于您之前获得的 token 。但是,我刚刚注意到您使用 https://gateway.watsonplatform.net/conversation/api URL 调用了 token API。如果您改为使用 https://gateway.watsonplatform.net/assistant/api 获取 token ,则您发布的代码应该可以正常工作。


由于 Conversation -> Assistant 重命名,使用新名称的授权似乎存在问题。因此,一旦获得授权 token ,就可以使用 setEndPoint() 方法调用 Conversation 端点,如下所示:

mAssistant = new Assistant("2018-02-16");
Map<String, String> map = new HashMap<>();
map.put("X-Watson-Authorization-Token", "{token_string}");
mAssistant.setDefaultHeaders(map);
mAssistant.setSkipAuthentication(true);

// change here
mAssistant.setEndPoint("https://gateway.watsonplatform.net/conversation/api");

Assistant 在内部只是 Conversation 的一个别名,因此您的 API 调用应该可以正常工作。

或者,您可以直接使用 Conversation 服务,尽管在某些时候它会消失,而只支持 Assistant 服务。

关于java - IBM Watson APIs Java SDK 使用 Watson token 认证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50209749/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com