尝试从 Dwolla restful api 获取访问令牌



我在获取沙盒环境的访问令牌时遇到问题。我按照本指南进行身份验证:OAuth

因此,当我按照本指南创建请求时,我从 API 收到以下响应: {"error":"access_denied","error_description":"Invalid application credentials."}

我按照说明使用 mf 客户端 ID 的密钥和密钥作为我的客户端密钥。

这是我使用的代码:

public static void main(String[] args) {
    try {
        URL url = new URL("https://www.dwolla.com/oauth/v2/token");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    conn.setRequestProperty("client_id", "<Key>");
            conn.setRequestProperty("client_secret", "<Secret>");
            conn.setRequestProperty("grant_type", "client_credentials"); 

            conn.setDoInput(true);
            conn.setDoOutput(true);

            System.out.println("Message:" + conn.getResponseMessage());
            BufferedReader br = new BufferedReader(new InputStreamReader(
            (conn.getInputStream())));
    String output;
    System.out.println("Output from Server .... n");
    while ((output = br.readLine()) != null) {
        System.out.println(output);
    }
    conn.disconnect();
    } catch (MalformedURLException ex) {
        Logger.getLogger(PaymentTest.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(PaymentTest.class.getName()).log(Level.SEVERE, null, ex);
    }
}

我终于能够获得访问令牌。我的问题是首先上面的代码使用 client_id 和 client_secret 作为标头参数。这些需要放在请求的正文中。

我的第二个问题是我为发送的消息使用了错误的内容类型。

这是对我有用的代码:

URL url = new URL("https://sandbox.dwolla.com/oauth/v2/token");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");

        conn.setDoInput(true);
        conn.setDoOutput(true);
        String data = "";

    JSONObject jsonObj = new JSONObject();
    jsonObj.put("client_id", "<Your Client ID>");
    jsonObj.put("client_secret", "<Your Client Secret>");
    jsonObj.put("grant_type", "client_credentials");
    data = jsonObj.toString();
    System.out.println("data = " + data);

    byte[] outputInBytes = data.getBytes("UTF-8");
    OutputStream os = conn.getOutputStream();
    os.write( outputInBytes );    
    os.close();

    System.out.println("Message:" + conn.getResponseMessage());

    BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
    String output;
    System.out.println("Output from Server .... n");
    while ((output = br.readLine()) != null) {
        System.out.println(output);
    }
    conn.disconnect();

相关内容

  • 没有找到相关文章

最新更新