2016-04-10 2 views
0

Я пытаюсь получить новый токен доступа для Oauth 2.0 с учетными данными клиента. Я всегда получаю Запретную или несанкционированную ошибку. В то время как я могу напрямую войти в URL-адрес https://api.flipkart.net/oauth-service/oauth/token?grant_type=client_credentials&scope=Seller_Api и генерировать маркер, но идти с ниже код я не в состоянии генерировать маркерНе удалось создать токен доступа для API-интерфейса flipkart

public static String getAccessToken(OAuth2Details oauthDetails) { 
     URL url; 
     HttpURLConnection con; 
     String accessToken = null; 
     try { 
      url = new URL("https://api.flipkart.net/oauth-service/oauth/token\?grant_type\=client_credentials\&scope=Seller_Api"); 
      con = (HttpURLConnection) url.openConnection(); 
      con.setRequestMethod("POST"); 
      con.setRequestProperty("Accept", "application/json"); 
      con.setDoOutput(true); 
      con.setDoInput(true); 
      String clientId = oauthDetails.getClientId(); 
      String clientSecret = oauthDetails.getClientSecret(); 
      String scope = oauthDetails.getScope(); 


       System.out 
         .println("Authorization server expects Basic authentication"); 

       con.setRequestProperty(
         OAuthConstants.AUTHORIZATION, 
         getBasicAuthorizationHeader(oauthDetails.getClientId(), 
           oauthDetails.getClientSecret())); 

       System.out.println("Retry with client credentials"); 

       int code = con.getResponseCode(); 
       System.out.print(con.getResponseMessage()); 
       BufferedReader br = new BufferedReader(new InputStreamReader(
         con.getErrorStream())); 

       if (code == 401 || code == 403) { 

        String s; 
        while ((s = br.readLine()) != null) { 
         System.out.print(br.readLine()); 
        } 
        con.disconnect(); 
        System.out 
          .println("Could not authenticate using client credentials."); 
        throw new RuntimeException(
          "Could not retrieve access token for client: " 
            + oauthDetails.getClientId()); 

       } 

      } 

      Map<String, String> map = handleResponse(con); 
      accessToken = map.get(OAuthConstants.ACCESS_TOKEN); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     return accessToken; 
    } 

    public static String getBasicAuthorizationHeader(String username, 
      String password) { 
     System.out.println("uu" + OAuthConstants.BASIC + " " 
       + encodeCredentials(username, password)); 
     return OAuthConstants.BASIC + " " 
       + encodeCredentials(username, password); 
    } 

    public static String encodeCredentials(String username, String password) { 
     String cred = username + ":" + password; 

     return new String(Base64.encodeBase64(cred.getBytes())); 

    } 

ответ

0

Удалить «\» из вашего URL

url = new URL("https://api.flipkart.net/oauth-service/oauth/token?grant_type=client_credentials&scope=Seller_Api"); 

сделать также приложение -ид и секретно это

<app-id>:<app_secret> 

example kdfjkfjdsakfjd93842908039489:kdjsfkajidsjf8939034820 

oauthDetails.getClientId()+":"+oauthDetails.getClientSecret() 
Смежные вопросы