2017-04-26 1 views
0

인증 코드를 얻은 후 아래 curl 명령을 사용하여 instagram api에서 액세스 토큰을 얻고 있습니다.데이터를 얻기 위해 curl 명령을 java http 클라이언트 코드로 변환하십시오.

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"} 

컬 URL :

https://api.instagram.com/oauth/access_token?client_id=cf07d1a2c69940e59420b6db4c936f4a&client_secret=fb0a975ca2024a1592459308df5ead47&grant_type=authorization_code&redirect_uri=http://localhost:8080/Insta_SMI_M1/auth&code=2c5d97c6d6454b8592816d7d39efb935

위의 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();  
} 

신체가 나를 도울 수 있습니까? 감사합니다.

+0

경우, 즉 귀하의 URL에 문제가 있음을 징조입니다. – betseyb

답변

1

GET 요청 양식 java에서 curl이 POST 요청을하고 있습니다. POST request using java (http-client 사용)을 만드는 방법에 대한 다음 예제를 따르십시오. 당신은 당신의 매개 변수를 설정하는 다음 코드 조각을 고려할 수 :

params.add(new BasicNameValuePair("client_id", "cf07d1a2c69940e59420b6db4c936f4a")); 
params.add(new BasicNameValuePair("client_secret", "fb0a975ca2024a1592459308df5ead47")); 
+0

힌트에 감사드립니다! (나는 자바 스프링 웹 휴식 API 인터 레이션에 익숙하지 않았기 때문에 나쁘게 그것을 필요로했다) –

0

내가 인터넷에 인스 타 그램 API의 액세스 토큰을 얻을 수있는 전체 코드를 찾을 수 din't 때문에, 나는 나를 위해 일하는 아래의 코드를 넣어 것입니다.

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"; 

} 

출력 : URL이 우체부에서 제대로 작동하지

{"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": ""}} 
관련 문제