【问题标题】:Unable to generate Access token for flipkart API无法为 Flipkart API 生成访问令牌
【发布时间】:2016-04-10 07:22:06
【问题描述】:

我正在尝试使用客户端凭据获取 Oauth 2.0 的新访问令牌。我总是遇到禁止或未经授权的错误。虽然我可以直接登录到 url https://api.flipkart.net/oauth-service/oauth/token?grant_type=client_credentials&scope=Seller_Api 并生成令牌,但使用下面的代码我无法生成令牌

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()));

    }

【问题讨论】:

    标签: oauth-2.0 httpurlconnection flipkart-api


    【解决方案1】:

    从你的网址中删除“\”

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

    也让 app-id 和 secret 像这样

    <app-id>:<app_secret>
    
    example kdfjkfjdsakfjd93842908039489:kdjsfkajidsjf8939034820
    
    oauthDetails.getClientId()+":"+oauthDetails.getClientSecret()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-06
      • 2022-07-17
      • 2023-01-03
      • 2015-11-16
      • 2020-08-11
      • 2018-11-10
      • 2023-04-04
      相关资源
      最近更新 更多