react-native - Oauth 2 密码授予流程无法与 axios 进行本机反应
问题描述
我正在尝试在反应本机应用程序中使用 OAuth 2.0 密码授予流程从 OAM 服务器获取令牌。但我总是得到 401 无效的客户端 ID 参数。
我试过邮递员,它正在工作,我也试过用java代码,它也在工作,但是当我尝试反应本机和axios时,我总是得到401无效的客户端ID参数错误。
正在工作的Java代码是
公共类TestService {
private static String ACCESS_TOKEN_URL =
"http://oauthservice/tokens";
//Redirect URI for Authorization flow
private static String REDIRECT_URI = "redirect_uri=http://ClientWebApp/index.html";
//Client Credentials
private static String CLIENT_ID = "xxxxxxxx";
private static String CLIENT_SECRET = "xxxxxxxxxxx";
private static String BASE_64_CREDENTIALS = "Basic " + new String(Base64.encode(CLIENT_ID + ":" + CLIENT_SECRET));
//Authorization and Token properties
private static String GRANT_TYPE = "grant_type=password";
private static String STATE = "state=getaccess";
private static String USERNAME = "username=xxxxxxx";
private static String PASSWORD = "password=xxxxxxx";
@GET
@Produces(MediaType.APPLICATION_JSON)
public String testService() {
String accessToken = null;
HttpURLConnection connection = null;
try {
System.out.println("###TestService called...: " );
//Constructs post Data
String params = GRANT_TYPE + "&" + USERNAME + "&" + PASSWORD;
byte[] postData = params.getBytes(Charset.forName("UTF-8"));
//Constructs URL and Connection objects, and sets headers
URL url = new URL(ACCESS_TOKEN_URL);
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Authorization", BASE_64_CREDENTIALS);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
connection.setRequestProperty("Accept-Charset", "UTF-8");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Length", Integer.toString(params.getBytes().length));
connection.setDoOutput(true);
//Gets response and reads it
OutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.write(postData);
wr.flush();
wr.close();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuffer resp = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
resp.append(inputLine);
}
in.close();
String res = resp.toString();
System.out.println("res: "+res );
//Obtains access token from JSON object
JSONObject obj = new JSONObject(res);
accessToken = obj.getString("access_token");
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
if (connection != null) {
connection.disconnect();
}
}
return accessToken;
}
}
React Native 代码(我尝试了很多方法,但没有一个有效)
1) axios.post(" http://oauthservice/tokens ", qs.stringify({ grant_type:"password", username:"xxxxxx", password:"xxxxxxx", scope:"xxxxxxxx" }), { '内容-type': 'application/x-www-form-urlencoded;charset=UTF-8', 'Authorization': "基本 xxxxxxxxxxxxxxxxxxx", })
.then((response) => {
alert(response)
})
.catch((error) => {
alert(JSON.stringify(error.response))
})
2)
axios({ method: 'post', url: 'oauthservice/tokens', headers: { 'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8', 'Authorization': "基本"+encoded_values, }, data: qs.stringify({
grant_type:"password", username:"xxxxxx", password:"xxxxx", scope:"xxxxxxxx" }) } ) .then((response) => { alert (响应) }) .catch((error) => { alert(JSON.stringify(error.response)) });
我尝试了许多第三方软件包,例如https://www.npmjs.com/package/axios-oauth-client
但他们也提供了同样的错误。
解决方案
推荐阅读
- keras - 使用完全卷积网络的直接热图回归
- azure - Terraform 能否解决 Azure 相互依赖问题,例如重命名资源?
- javascript - 如何创建一个可以通过按钮添加和删除输入的列表?
- foreach - SwiftUI ForEach 没有动画延迟
- javascript - Karma 测试:在公司代理后面加载 JS 文件时出错
- typescript - 在本机反应中扩展通用道具
- python - 在读取字符串的每一行后尝试打印列表元素
- facebook - 如何使用 Facebook Graph API 请求 pages_show_list 权限?
- javascript - 删除字符串中重复的连续单词的最快算法?
- django - Django 教程中的结构与我的结构不匹配