【问题标题】:How to fetch token from Token API using Java如何使用 Java 从 Token API 获取令牌
【发布时间】:2022-02-11 16:57:46
【问题描述】:

我正在尝试通过调用令牌 API 来获取令牌,但无法获取相同的。 它在邮递员中工作。 邮递员详情如下

我的代码

import java.io.*;
import java.net.*;
public class GetToken {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        String accessToken = "";
        String tokenURL="https://test.iapsoftware.com/iap6/MobileServices/token";
        String grantType = "password";
        String userName="APITester";
        String password="password"; 
        String ClientCode ="iaptest1";
        String ClientInterface ="API";
        
                try {
                     URL url = new URL(tokenURL);
                    HttpURLConnection httpConn = (HttpURLConnection)url.openConnection();
                    // Set header
                    httpConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
                    httpConn.setRequestProperty("ClientCode", ClientCode);
                    httpConn.setRequestProperty("ClientInterface", ClientInterface);
                    
                    //httpConn.setRequestBody();
                    httpConn.setDoOutput(true);
                    httpConn.setDoInput(true);
                    httpConn.setRequestMethod("POST"); 
                    //Set Request Body
                    String jsonInputString =("grant_type="+grantType+",username="+userName+",password="+password);
                            
                    OutputStream os = httpConn.getOutputStream();
                    byte[] input = jsonInputString.getBytes("utf-8");
                    os.write(input, 0, input.length);            
                     // Read the response.
                    InputStreamReader isr=null;
                        if (httpConn.getResponseCode() == 200) {
                     isr = new InputStreamReader(httpConn.getInputStream());
                    }
                            else
                         {
                                               isr = new InputStreamReader(httpConn.getErrorStream());
                   }
                    BufferedReader in = new BufferedReader(isr);
                    String responseString = "";
                    String outputString = "";
                   // Write response to a String.
                    while ((responseString = in.readLine()) != null) {
                     outputString = outputString + responseString;
                    }
                       accessToken = outputString;
                } 
                catch (Exception e) 
                {
                    accessToken = "Error"; //+ e.getMessage();            
                }

                System.out.println(accessToken);
        
    }

}

错误

-据我了解,我没有传递正确的输入。 输入需要进行 URL 编码,我将其作为 JSON 发送。 请提出建议并提供解决方案。

【问题讨论】:

  • 可能你的帖子正文有问题。它没有捕捉到您发送的授权类型

标签: java oauth user-defined-functions access-token urlencode


【解决方案1】:

正确设置主体后,它可以工作

String urlParameters  = ("grant_type="+grantType+"&username="+userName+"&password="+password);
                                            
OutputStream os = httpConn.getOutputStream();
                    
byte[] postData = urlParameters.getBytes("utf-8");
int postDataLength = postData.length;
                    
os.write(postData, 0, postDataLength);  
 

【讨论】:

    猜你喜欢
    • 2016-11-14
    • 1970-01-01
    • 1970-01-01
    • 2019-03-25
    • 2013-07-20
    • 2017-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多