当前位置: 首页 > 知识库问答 >
问题:

尝试从Azure AD获取访问令牌时获取响应代码400

支才
2023-03-14

我正在为我的web应用程序实现azure,并尝试通过下面的openId connect教程获得访问令牌

内容类型:application/x-www-form-urlencoded

GRANT_TYPE=授权代码

client_id=2D4D11A2-F814-46A7-890A-274A72A7309E

public static String post( String endpoint,
        Map<String, String> params) {//YD
    StringBuffer paramString = new StringBuffer("");
    //if(!Utilities.checkInternetConnection(context)){
    //  return XMLHandler.getXMLForErrorCode(context, JSONHandler.ERROR_CODE_INTERNET_CONNECTION);
    //}
    Iterator<Entry<String, String>> iterator = params.entrySet().iterator();
    StringBuffer tempBuffer = new StringBuffer("");
    String paramval;
    while (iterator.hasNext()) {
        Entry<String, String> param = iterator.next();
        if (param != null) {
            if (paramString.length() > 0) {
                paramString.append("&");
            }
            System.out.println( "post key : " + param.getKey());
            String value;
            try {
                paramval = param.getValue();
                if(paramval!=null)
                    value = URLEncoder.encode(paramval, "UTF-8");
                else
                    value = "";
            } catch (UnsupportedEncodingException e) {
                value = "";
                e.printStackTrace();
            }


                paramString.append(param.getKey()).append("=")
                        .append(value);
        }
    }
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(endpoint);
        String data = "";

        try {
            // Add your data
            // httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs))
            //httppost.addHeader("Host", host);
            httppost.addHeader("Content-Type",
                    "application/x-www-form-urlencoded");

            if (!paramString.equals("")) {
                if (tempBuffer.length() > 0) {
                    data = data + tempBuffer.toString();
                }
                data = data + paramString.toString();

                if (data.endsWith("&")) {
                    data = data.substring(0, data.length() - 1);
                } 

                httppost.setEntity(new ByteArrayEntity(data.getBytes()));
            }

            System.out.println( "post Stringbuffer  : " + data);

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);
            int statuscode = response.getStatusLine().getStatusCode();
            System.out.println("Response code : " + statuscode);
            if (statuscode != 200) {
                return null;
            }
            HttpEntity entity = response.getEntity();
            InputStream in = null;
            if (entity != null) {
                in = entity.getContent();
            }

            if (in != null) {
                StringBuilder builder = new StringBuilder();
                String line;
                try {
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(in, "UTF-8"));
                    while ((line = reader.readLine()) != null) {
                        builder.append(line);
                    }
                } finally {
                    in.close();
                }

                String response2 = builder.toString();

                System.out.println("response :" + response2);
                retrycount = 0;

                return response2;
            }
        }
        catch(UnknownHostException e){
            e.printStackTrace();
            return null;
        }
        catch (EOFException eof) {
            if (retrycount < max_retry) {
                eof.printStackTrace();
                post( endpoint, params);
                retrycount = 1;
            }
        } catch (Throwable th) {
            throw new IOException("Error in posting :" + th.getMessage());
        }
        retrycount = 0;
        return null;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

请帮帮我

提前致谢

共有1个答案

百里修真
2023-03-14

是否确保传递给/token的重定向uri与传递给/authorize的uri相同

我相信,如果你能用你当前的客户端id、秘密和范围使用Postman工具来测试OAuth身份验证代码流,以便排除不良配置,这将会有所帮助。

 类似资料:
  • 我可以获得帐户ID为#10540382的JWT访问令牌,这是我为我们公司的电子签名实现创建的。我用这个帐户完成了JWT实现,一切顺利。但是现在公司提供了我的新帐户ID#11024495。但是有了这个新帐户,我没有获得访问令牌。令牌APIhttps://account-d.docusign.com/oauth/token现在返回“远程服务器返回错误:(400)错误请求”。 DocuSign的人能帮我

  • 我正在尝试使用spotipy向特定用户验证我的python应用程序。我以前和其他一些用户做过这件事,没有任何问题。然而,每当我尝试使用这个用户时,我总是得到“{”错误“:“无效的授权”,“错误描述“:“无效的授权码”}” 我还尝试了几种手动方式,包括卷曲: curl-H“授权:基本OTQxNDliNjI3…WRhMzk5YTY2ZTliZWY=“-d授权类型=授权代码-d代码=AQByvdq4Vr

  • 获取访问令牌的文档中的第一步是“将用户引导到我们的授权URL”。那到底是什么意思?没有提供链接或任何东西。它还说“公司名称、联系电子邮件和隐私政策URL是开始提交的必要条件。”我们的应用程序没有隐私政策...只是一个简单的标签提要。我不明白为什么有一个简单的标签提要那么复杂。 是否有一个等待时间来获得批准的应用程序..如果它得到批准...在获得访问令牌之前,我必须获得批准吗?这在任何地方都没有概述

  • 我使用Azure AD作为具有IdentityServer4的外部IdP。要调用受AzureAd保护的API,我需要从Azure Ad获取访问令牌。是否可以在登录过程中获取访问令牌并将其保存到声明中? 我正在使用IdentityServer4快速启动UI。我试图在外部令牌的回调方法中捕获访问令牌,但在HttpContext或声明或ProcessLoginCallbackForOidc方法中没有找到

  • 我正在尝试在多租户应用程序中实现 OAuth 2.0 授权代码流。 我首先在 请求了“openid 配置文件电子邮件”范围的授权,然后使用我获得的代码以及 、client_id、client_secret redirect_uri 作为访问令牌 我得到的响应只包含< code>id_token和< code>token_type=Bearer。为什么没有给我< code>access_token和

  • null 我得到以下错误: 我做错了什么?