因此,我正在尝试为用户制作一个应用程序来刷信用卡,它将蓝牙连接到设备来处理它。我正在使用PayPal SDK,但我似乎无法弄清楚如何获取访问令牌用于我的请求。这是我目前的代码:
public String performPostCall(String requestURL, HashMap<String, String> postDataParams) {
URL url;
String response = "";
try {
url = new URL(requestURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("Accept", "application/json");
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode=conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
String line;
BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line=br.readLine()) != null) {
response+=line;
}
}
else {
response="";
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for(Map.Entry<String, String> entry : params.entrySet()){
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
return result.toString();
}
public String getPaypalToken() {
HashMap<String, String> params = new HashMap<String, String>();
String CLIENT_ID = "no";
String SECRET = "no";
params.put("username", Base64.encodeToString((CLIENT_ID + ":" + SECRET).getBytes(), Base64.DEFAULT));
params.put("grant_type", "client_credentials");
System.out.println("Params: " + params);
String output = performPostCall("https://api.sandbox.paypal.com/v1/oauth2/token", params);
System.out.println("Output: " + output);
return output;
}
我知道将client_id
和secret
置于Android设备本身是非常不安全的,但此应用程序唯一的用途就是我的设备。 PayPal什么也没回来,我也不知道为什么。如果有人能给我一些指针,甚至可能是一些非常好的代码。 :)