【问题标题】:Unable to Obtain eBay OAuth Access Token无法获取 eBay OAuth 访问令牌
【发布时间】:2019-06-02 01:48:50
【问题描述】:

我正在尝试获取 eBay OAuth 访问和刷新令牌,但不断收到 401 Unauthorized 响应。我已经阅读了有关这方面的所有文档,并且几乎尝试了所有方法,但没有任何乐趣。

我已经完成了用户权限流程,授予了对我的应用程序的访问权限并拥有了授权代码 - 我现在手动将其粘贴到我的代码中,并尝试了 URL 编码和 URL 解码,但结果相同。

我不确定问题出在我的 java 代码还是我的 eBay 开发人员帐户中的某个值。任何想法或指示都将受到欢迎。

public int initialiseToken(String clientID, String clientSecret, String ruName)
{
    int responseCode = 0;
    try
    {
            String urlString = "https://api.ebay.com/identity/v1/oauth2/token";
            String clientCredentials = clientID + ":" + clientSecret;
            // base64 encode credentials
            byte[] base64clientCredentials = Base64.encodeBase64(clientCredentials.getBytes());

            // below authCode obtained from URI redirect following eBay auth sign-in 
            String authCodeURLEncoded = "v%5E1.1%23i%5E1%23I%5E3%23f.....xI0VeMjYw";
            String authCodeURLDecoded = URLDecoder.decode(authCodeURLEncoded, "UTF-8");
            
            URL url = new URL(urlString);
            HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Authorization", "Basic " + base64clientCredentials);
            conn.setRequestProperty("Accept", "application/json");
            conn.setRequestProperty("Accept-Charset", "utf8");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            
            conn.setRequestProperty("grant_type", "authorization_code");
            conn.setRequestProperty("redirect_uri", ruName);
            conn.setRequestProperty("code", authCodeURLDecoded);         // have tried both encoded & decoded versions
            
            String msg;
            if (conn.getResponseCode() != 200) 
            {
                    responseCode = conn.getResponseCode();
                    msg = conn.getResponseMessage();
            }
            else
            {
                    responseCode = conn.getResponseCode();
                    msg = conn.getResponseMessage();
                    BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
                    String line = br.readLine();
                    parseResult(line);
            }
    }
    catch (MalformedURLException e) 
    {
            e.printStackTrace();
    } 
    catch (IOException e) 
    {
            e.printStackTrace();
    }
    catch (Exception e) 
    {
            e.printStackTrace();
    }
    return responseCode;
}

【问题讨论】:

    标签: java oauth ebay-api


    【解决方案1】:

    如果有帮助,我有一段代码用于 OAuth 客户端凭据(用于访问一般信息)。

    我正在做一些非常相似的事情。我现在坚持使用返回的访问令牌来实际实现 ebay 方法。如果你成功了,可以告诉我吗?

    依赖

    implementation 'com.squareup.okhttp3:okhttp:3.5.0'
    

    代码

    公共类 MainActivity 扩展 AppCompatActivity {

    private static final String clientId = "-";//clientId
    private static final String clientSecret = "-";//client secret
    private static final String tokenUrl = "https://api.sandbox.ebay.com/identity/v1/oauth2/token";
    private static final String auth = clientId + ":" + clientSecret;
    private static final String authentication = Base64.encodeToString(auth.getBytes(),Base64.NO_WRAP);
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .addHeader("Authorization", "Basic " + authentication)
                .url(tokenUrl)
                .post(RequestBody.create(MediaType.parse("application/x-www-form-urlencoded")
                        ,"grant_type=client_credentials"+
                                "&scope=https://api.ebay.com/oauth/api_scope"
                        )
                )
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("Testing_", "Error: " +e.getMessage());
            }
    
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String json = response.body().string();
    
                JSONObject data = null;
                try {
                    data = new JSONObject(json);
                    String accessToken = data.optString("access_token");
                    String refreshToken = data.optString("refresh_token");
    
                    Log.d("Testing_", "Access Token = " + accessToken);
                    Log.d("Testing_", "Refresh Token = " + refreshToken);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    

    Output

    【讨论】:

      【解决方案2】:

      谢谢迈克尔 - 当我再次拿起这个时会看看。同时,这里有一些我用来测试搜索取消界面的代码——希望它对你有用

          public static void searchCancellations(String authToken)
      {
          String urlString = "https://api.ebay.com/post-order/v2/cancellation/search";
          String urlStringParams = "?creation_date_range_from=2018-12-01T00:00:00.000Z&creation_date_range_to=2019-01-16T00:00:00.000Z";
          try
          {
              String encodedUrl = urlString + URLEncoder.encode(urlStringParams, "UTF-8");
              URL url = new URL(encodedUrl);
              HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
              conn.setRequestMethod("GET");
              conn.setRequestProperty("Accept", "application/json");
              conn.setRequestProperty("Accept-Charset", "utf8");
              conn.setRequestProperty("Authorization", "IAF " + authToken);
              conn.setRequestProperty("Content-Type", "application/json");
      
              int responseCode = 0;
              String msg;
              if (conn.getResponseCode() != 200) 
              {
                  responseCode = conn.getResponseCode();
                  msg = conn.getResponseMessage();
              }
              else
              {
                  responseCode = conn.getResponseCode();
                  msg = conn.getResponseMessage();
              }
              BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
              String line = br.readLine();
              parseCancellationResult(line);
              responseCode = 0;
          }
          catch (MalformedURLException e) 
          {
              e.printStackTrace();
          } 
          catch (IOException e) 
          {
              e.printStackTrace();
          }
          catch (Exception e) 
          {
              e.printStackTrace();
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-10-18
        • 1970-01-01
        • 1970-01-01
        • 2017-06-23
        • 2019-03-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多