将一个curl命令转换成javahttp客户端代码获取数据
Convert a curl command to java http client code to get data
我正在使用下面的 curl 命令在获取授权码后从 instagram api 获取访问令牌。
curl \-F 'client_id=cf07d1a2c69940e59420b6db4c936f4a' \
-F 'client_secret=fb0a975ca2024a1592459308df5ead47' \
-F 'grant_type=authorization_code' \
-F 'redirect_uri=http://localhost:8080/Insta_SMI_M1/accessToken/' \
-F 'code=fcf66e5f09bf43a18ab15e5f1e0ae75f'
\https://api.instagram.com/oauth/access_token/
输出:
{"access_token": "5351945621.cf07d1a.1d35647e22f24ed0885f65545f3f1b0b", "user": {"id": "5351945621", "username": "abhaykumar", "profile_picture": "https://scontent-amt2-1.cdninstagram.com/t51.2885-19/11906329_960233084022564_1448528159_a.jpg", "full_name": "Quantum Four", "bio"}
Curl Url:
以上url在邮递员或浏览器中使用时既没有给出任何错误也没有显示输出(它的空行)。
下面是相同的代码。
@RequestMapping(value="/auth", method=RequestMethod.GET)
public String getAuthCode(HttpServletRequest request, HttpServletResponse response)
{
String code = request.getParameter("code");
System.out.println("code is: "+ code);
String url = "https://api.instagram.com/oauth/access_token?"
+ "client_id=" + Constants.CLIENT_ID
+ "&client_secret=" + Constants.CLIENT_SECRET
+ "&grant_type=authorization_code"
+ "&redirect_uri=" + Constants.REDIRECT_URI_AUTH
+ "&code="+code;
System.out.println("Access Token URL: "+ url);
StringBuffer result = null;
try {
System.out.println("1");
@SuppressWarnings({ "resource", "deprecation" })
HttpClient client = new DefaultHttpClient();
HttpGet request1 = new HttpGet(url);
System.out.println("2");
HttpResponse response1 = client.execute(request1);
BufferedReader rd = new BufferedReader(
new InputStreamReader(response1.getEntity().getContent()));
System.out.println("3");
result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
System.out.println("line " + line);
result.append(line);
System.out.println("3");
}
} catch (UnsupportedOperationException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(result.toString());
return result.toString();
}
任何人都可以帮助我吗?
谢谢
curl 正在执行 POST 请求,而您正在执行 GET 请求表单 java。按照此示例了解如何制作 POST request using java(使用 http 客户端)。您可以考虑使用以下代码来设置您的参数:
params.add(new BasicNameValuePair("client_id", "cf07d1a2c69940e59420b6db4c936f4a"));
params.add(new BasicNameValuePair("client_secret", "fb0a975ca2024a1592459308df5ead47"));
由于我在互联网上找不到从 instagram api 获取访问令牌的完整代码,所以我将把对我有用的代码放在下面。
public String accessTkn (String code)
{
try {
HttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new
HttpPost("https://api.instagram.com/oauth/access_token");
// Request parameters and other properties.
List<NameValuePair> params = new ArrayList<NameValuePair>(2);
params.add(new BasicNameValuePair("client_id", Constants.CLIENT_ID));
params.add(new BasicNameValuePair("client_secret", Constants.CLIENT_SECRET));
params.add(new BasicNameValuePair("grant_type", "authorization_code"));
params.add(new BasicNameValuePair("redirect_uri", Constants.REDIRECT_URI_AUTH));
params.add(new BasicNameValuePair("code", code));
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
//Execute and get the response.
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
System.out.println("entity "+ entity.getContent());
if (entity != null) {
InputStream instream = entity.getContent();
try {
return (getStringfromStream(instream));
// do something useful
} finally {
instream.close();
}
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedOperationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "Abhay";
}
输出:
{"access_token": "5351945621.cf07d1a.1d35647e22f24ed0885f65545f3f1b0b", "user": {"id": "5351945621", "username": "abhaykumar", "profile_picture": "https://instagram.fsgn2-1.fna.fbcdn.net/t51.2885-19/11906329_960233084022564_1448528159_a.jpg", "full_name": "Abhay Kumar", "bio": "", "website": ""}}
我正在使用下面的 curl 命令在获取授权码后从 instagram api 获取访问令牌。
curl \-F 'client_id=cf07d1a2c69940e59420b6db4c936f4a' \
-F 'client_secret=fb0a975ca2024a1592459308df5ead47' \
-F 'grant_type=authorization_code' \
-F 'redirect_uri=http://localhost:8080/Insta_SMI_M1/accessToken/' \
-F 'code=fcf66e5f09bf43a18ab15e5f1e0ae75f'
\https://api.instagram.com/oauth/access_token/
输出:
{"access_token": "5351945621.cf07d1a.1d35647e22f24ed0885f65545f3f1b0b", "user": {"id": "5351945621", "username": "abhaykumar", "profile_picture": "https://scontent-amt2-1.cdninstagram.com/t51.2885-19/11906329_960233084022564_1448528159_a.jpg", "full_name": "Quantum Four", "bio"}
Curl Url:
以上url在邮递员或浏览器中使用时既没有给出任何错误也没有显示输出(它的空行)。
下面是相同的代码。
@RequestMapping(value="/auth", method=RequestMethod.GET)
public String getAuthCode(HttpServletRequest request, HttpServletResponse response)
{
String code = request.getParameter("code");
System.out.println("code is: "+ code);
String url = "https://api.instagram.com/oauth/access_token?"
+ "client_id=" + Constants.CLIENT_ID
+ "&client_secret=" + Constants.CLIENT_SECRET
+ "&grant_type=authorization_code"
+ "&redirect_uri=" + Constants.REDIRECT_URI_AUTH
+ "&code="+code;
System.out.println("Access Token URL: "+ url);
StringBuffer result = null;
try {
System.out.println("1");
@SuppressWarnings({ "resource", "deprecation" })
HttpClient client = new DefaultHttpClient();
HttpGet request1 = new HttpGet(url);
System.out.println("2");
HttpResponse response1 = client.execute(request1);
BufferedReader rd = new BufferedReader(
new InputStreamReader(response1.getEntity().getContent()));
System.out.println("3");
result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
System.out.println("line " + line);
result.append(line);
System.out.println("3");
}
} catch (UnsupportedOperationException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(result.toString());
return result.toString();
}
任何人都可以帮助我吗? 谢谢
curl 正在执行 POST 请求,而您正在执行 GET 请求表单 java。按照此示例了解如何制作 POST request using java(使用 http 客户端)。您可以考虑使用以下代码来设置您的参数:
params.add(new BasicNameValuePair("client_id", "cf07d1a2c69940e59420b6db4c936f4a"));
params.add(new BasicNameValuePair("client_secret", "fb0a975ca2024a1592459308df5ead47"));
由于我在互联网上找不到从 instagram api 获取访问令牌的完整代码,所以我将把对我有用的代码放在下面。
public String accessTkn (String code)
{
try {
HttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new
HttpPost("https://api.instagram.com/oauth/access_token");
// Request parameters and other properties.
List<NameValuePair> params = new ArrayList<NameValuePair>(2);
params.add(new BasicNameValuePair("client_id", Constants.CLIENT_ID));
params.add(new BasicNameValuePair("client_secret", Constants.CLIENT_SECRET));
params.add(new BasicNameValuePair("grant_type", "authorization_code"));
params.add(new BasicNameValuePair("redirect_uri", Constants.REDIRECT_URI_AUTH));
params.add(new BasicNameValuePair("code", code));
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
//Execute and get the response.
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
System.out.println("entity "+ entity.getContent());
if (entity != null) {
InputStream instream = entity.getContent();
try {
return (getStringfromStream(instream));
// do something useful
} finally {
instream.close();
}
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedOperationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "Abhay";
}
输出:
{"access_token": "5351945621.cf07d1a.1d35647e22f24ed0885f65545f3f1b0b", "user": {"id": "5351945621", "username": "abhaykumar", "profile_picture": "https://instagram.fsgn2-1.fna.fbcdn.net/t51.2885-19/11906329_960233084022564_1448528159_a.jpg", "full_name": "Abhay Kumar", "bio": "", "website": ""}}