首页 > 解决方案 > 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

但他们也提供了同样的错误。

标签: react-nativeoauth-2.0axios

解决方案


推荐阅读