Oauth 2密码授予流程无法与axios本机交互

时间:2019-06-21 15:39:13

标签: react-native oauth-2.0 axios

我试图在React Native应用程序中使用OAuth 2.0密码授予流从OAM服务器获取令牌,但是我总是会获得401无效的客户端ID参数。

我已经尝试过邮递员,并且它也能正常工作,而且我尝试使用Java代码也能工作,但是当iam尝试响应native和axios iam时,总是会收到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;
}

}

反应本机代码(我已经尝试了很多方法,但是都没有用)

1) axios.post(“ http://oauthservice/tokens”, qs.stringify({grant_type:“ password”,           用户名:“ xxxxxx”,           密码:“ xxxxxxx”,           范围:“ xxxxxxxx”}),{             '内容类型':'应用程序/ x-www-form-urlencoded; charset = UTF-8',             “授权”:“基本xxxxxxxxxxxxxxxxxxx”,         })

  .then((response) => {
  alert(response)
  })
  .catch((error) => {
     alert(JSON.stringify(error.response))
  })

2)

axios({         方法:“发布”,         网址:“ oauthservice /令牌”,         标头:{           '内容类型':'应用程序/ x-www-form-urlencoded; charset = UTF-8',           '授权':“基本” + encoded_values,         },         数据:qs.stringify({
          grant_type:“密码”,           用户名:“ xxxxxx”,           密码:“ xxxxx”,           范围:“ xxxxxxxx”         })       }       ).then((response)=> {         警报(响应)         })         .catch((错误)=> {            警报(JSON.stringify(error.response))         });

我尝试了许多第三方软件包,例如https://www.npmjs.com/package/axios-oauth-client

但是它们也提供相同的错误。

0 个答案:

没有答案