【问题标题】:Missing Grant Type in Post method In Android在 Android 中的 Post 方法中缺少授权类型
【发布时间】:2019-05-05 10:51:38
【问题描述】:

在使用 Post 方法时在 Android 中我的 grant type 在缺失

我想从我的 post 方法中获取 access_token,但它显示 grant_type 缺失,因为在正文中我的 grant_type 是 client_credentials 所以我如何将 grant_type 添加到我的 url 以便我授予 access_token 权限

 Request originalRequest = chain.request();

 String credentials = authUserName + ":" + authPassword;

 final String basic =Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);

 Request newRequest = chain.request().newBuilder().addHeader("Authorization","Basic  " + basic).build();
                    return chain.proceed(newRequest);

我的apiInterface

@POST("token")
Call<ResponseBody> getToken();

}

我的日志报告

D/OkHttp: {"error":"invalid_request","error_description":"Missing grant type"}

Postman Post 方法截图

如何在我的代码中授予类型

【问题讨论】:

    标签: android json api postman


    【解决方案1】:

    你到底想做什么。从代码看来,您正在使用 OAuth2 在应用程序中进行身份验证 因此,请在发送请求之前发布最后形成的完整 url。 使用 OAuth2 时,您需要将参数 grant_typeredirect URL 传递到服务器中,当您在身份验证服务器中注册您的应用程序时。授权类型可以有可能的值,如 codeimplicit 祝你好运

    【讨论】:

    • 我想从我的 post 方法中获取 access_token,但它显示 grant_type 丢失,因为在正文中我的 grant_type 是 client_credentials 所以我如何将grant_type 添加到我的 url 以便我授予 access_token 权限
    【解决方案2】:
     String credentials = authUserName + ":" + authPassword;
                        final String basic = Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
                        RequestBody body = RequestBody.create(MediaType.parse("application/x-www-form-urlencoded"), "grant_type=client_credentials");
                        Request newRequest = chain
                                .request()
                                .newBuilder()
                                .addHeader(HTTP_AUTH_HEADER, "Basic  " + basic)
                                .addHeader("Content_Type", "application/x-www-form-urlencoded")
                                .post(body).build();
    

    【讨论】:

      猜你喜欢
      • 2017-12-11
      • 2017-01-14
      • 1970-01-01
      • 2014-12-16
      • 2020-11-12
      • 2018-09-11
      • 1970-01-01
      • 2018-05-03
      • 1970-01-01
      相关资源
      最近更新 更多