|
@@ -0,0 +1,193 @@
|
|
|
+package com.raycos.raycoswebapi.amazon.authorize;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Author: xiaojiabin
|
|
|
+ * @Date: 2024/8/31 17:03
|
|
|
+ */
|
|
|
+
|
|
|
+public class AmazonAuthorize {
|
|
|
+
|
|
|
+
|
|
|
+ // 用户通过页面调用Amazon授权 接口
|
|
|
+ // 授权成功后 亚马逊回调 我的接口 入参:
|
|
|
+ /**
|
|
|
+ * state 步骤 2 中的 state 值。
|
|
|
+ * selling_partner_id 授权您的应用程序的销售合作伙伴的标识符。
|
|
|
+ * mws_auth_token (仅限卖家应用程序)您在为 Amazon Marketplace Web Service 调用创建查询字符串时指定的值。
|
|
|
+ * spapi_oauth_code 用于生成 LWA 刷新令牌的 Login with Amazon (LWA) 授权码(请参阅第 5 步)。
|
|
|
+ */
|
|
|
+ // 我拿到 spapi_oauth_code 去调用刷新令牌接口
|
|
|
+ // 发送:
|
|
|
+ /**
|
|
|
+ * grant_type 请求的访问授权的类型。必须是 。authorization_code
|
|
|
+ * code 您在步骤 4 中收到的 LWA 授权码。Amazon 会向您发送授权信息。
|
|
|
+ * redirect_uri 应用程序的重定向 URI。
|
|
|
+ * client_id LWA 凭证的一部分。要获取此值,请参阅 查看您的开发者信息。
|
|
|
+ * client_secret LWA 凭证的一部分。要获取此值,请参阅 查看您的开发者信息。
|
|
|
+ */
|
|
|
+ // 接收:
|
|
|
+ /**
|
|
|
+ * access_token 一个令牌,用于授权您的应用程序代表销售合作伙伴执行某些操作。有关详细信息,请参阅连接到 SP-API。
|
|
|
+ * token_type 返回的 token 类型。应该是 bearer。
|
|
|
+ * expires_in 访问令牌失效前的秒数。
|
|
|
+ * refresh_token 可用于生成新访问令牌的长寿命令牌。有关详细信息,请参阅连接到 SP-API。
|
|
|
+ */
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+//
|
|
|
+// private static String client_id;
|
|
|
+//
|
|
|
+// public static String getAccessToken(String code) {
|
|
|
+// try {
|
|
|
+//
|
|
|
+// String url = "https://api.amazon.com/auth/o2/token";
|
|
|
+// Map<String, String> map = new HashMap<>();
|
|
|
+// map.put("grant_type", "authorization_code");
|
|
|
+// map.put("code", code);
|
|
|
+// map.put("client_id", client_id);
|
|
|
+// map.put("redirect_uri", "https://api.mabangerp.com/fba/api/v1/authCode");
|
|
|
+// map.put("client_secret", client_secret);
|
|
|
+// Map<String, String> headerMap = new HashMap<>();
|
|
|
+// logger.info(JSONObject.toJSONString(map));
|
|
|
+// headerMap.put("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
|
|
|
+// String response = HttpClientUtil.httpPostForm(url, map, headerMap, null, 5000);
|
|
|
+//
|
|
|
+// logger.info(", get accessToken response : " + response);
|
|
|
+// return response;
|
|
|
+// } catch (Exception e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// logger.error(e.getMessage(), e);
|
|
|
+// }
|
|
|
+// return "报错";
|
|
|
+// }
|
|
|
+//
|
|
|
+// public static String refreshAccessToken(String refresh_token) {
|
|
|
+// try {
|
|
|
+// String url = "https://api.amazon.com/auth/o2/token";
|
|
|
+// Map<String, String> map = new HashMap<>();
|
|
|
+// map.put("grant_type", "refresh_token");
|
|
|
+// map.put("refresh_token", refresh_token);
|
|
|
+// map.put("client_id", client_id);
|
|
|
+// map.put("client_secret", client_secret);
|
|
|
+// Map<String, String> headerMap = new HashMap<>();
|
|
|
+// logger.info(JSONObject.toJSONString(map));
|
|
|
+// headerMap.put("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
|
|
|
+// String response = HttpClientUtil.httpPostForm(url, map, headerMap, null, 5000);
|
|
|
+// logger.info("refresh access_token response : " + response);
|
|
|
+// return response;
|
|
|
+// } catch (Exception e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// logger.error(e.getMessage(), e);
|
|
|
+// }
|
|
|
+// return "报错";
|
|
|
+// }
|
|
|
+//
|
|
|
+//
|
|
|
+// public static String httpPostForm(String requestUrl, Map<String, String> paramMap,
|
|
|
+// Map<String, String> headerMap, String encode, int timeOut) {
|
|
|
+// String response = "";
|
|
|
+// if (encode == null) {
|
|
|
+// encode = "utf-8";
|
|
|
+// }
|
|
|
+//
|
|
|
+// // HttpClients.createDefault() 等价于 HttpClientBuilder.create().build();
|
|
|
+// CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
|
|
|
+// HttpPost httpPost = new HttpPost(requestUrl);
|
|
|
+//
|
|
|
+// // 设置超时时间
|
|
|
+// RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeOut)
|
|
|
+// .setConnectTimeout(timeOut).setSocketTimeout(timeOut).build();
|
|
|
+// httpPost.setConfig(requestConfig);
|
|
|
+//
|
|
|
+// // 设置header
|
|
|
+// if (headerMap != null && headerMap.size() > 0) {
|
|
|
+// for (String key : headerMap.keySet()) {
|
|
|
+// httpPost.setHeader(key, headerMap.get(key));
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 设置Param
|
|
|
+// if (paramMap != null && paramMap.size() > 0) {
|
|
|
+//
|
|
|
+// List<NameValuePair> paramList = new ArrayList<NameValuePair>();
|
|
|
+// for (String key : paramMap.keySet()) {
|
|
|
+// paramList.add(new BasicNameValuePair(key, paramMap.get(key)));
|
|
|
+// }
|
|
|
+//
|
|
|
+// try {
|
|
|
+// httpPost.setEntity(new UrlEncodedFormEntity(paramList, encode));
|
|
|
+// } catch (UnsupportedEncodingException e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+//
|
|
|
+// }
|
|
|
+//
|
|
|
+// CloseableHttpResponse httpResponse = null;
|
|
|
+// try {
|
|
|
+// httpResponse = closeableHttpClient.execute(httpPost);
|
|
|
+// HttpEntity entity = httpResponse.getEntity();
|
|
|
+// byte[] bytes = (entity == null ? null : EntityUtils.toByteArray(entity));
|
|
|
+// String content = (bytes == null ? null : new String(bytes, encode));
|
|
|
+// response = content;
|
|
|
+// } catch (Exception e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// } finally {
|
|
|
+// try {
|
|
|
+// if (httpResponse != null) {
|
|
|
+// httpResponse.close();
|
|
|
+// }
|
|
|
+// } catch (IOException e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// try {
|
|
|
+// // 关闭连接、释放资源
|
|
|
+// if (closeableHttpClient != null) {
|
|
|
+// closeableHttpClient.close();
|
|
|
+// }
|
|
|
+// } catch (IOException e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+//
|
|
|
+// return response;
|
|
|
+// }
|
|
|
+//
|
|
|
+// /**
|
|
|
+//
|
|
|
+// https://sellingpartnerapi-na.amazon.com/apps/authorize/consent
|
|
|
+// https://sellingpartnerapi-eu.amazon.com/apps/authorize/consent
|
|
|
+// https://sellingpartnerapi-fe.amazon.com/apps/authorize/consent
|
|
|
+//
|
|
|
+// https://www.amazon.com/ap/oa?client_id=AKIAJQM2XW6X65DC5OWA
|
|
|
+// &scope=profile
|
|
|
+// &response_type=code
|
|
|
+// &state=208257577ll0975l93l2l59l895857093449424
|
|
|
+// &redirect_uri=https://client.example.com/auth_popup/token
|
|
|
+// &code_challenge=Fw7s3XHRVb2m1nT7s646UrYiYLMJ54as0ZIU_injyqw
|
|
|
+// &code_challenge_method=S256
|
|
|
+//
|
|
|
+// if ($this->site == 'eu') {
|
|
|
+// return 'https://sellercentral-europe.amazon.com/apps/authorize/consent';
|
|
|
+// } elseif ($this->site == 'fe') {
|
|
|
+// if ($this->countryCode == 'au') {
|
|
|
+// return 'https://sellercentral.amazon.com.au/apps/authorize/consent';
|
|
|
+// } elseif ($this->countryCode == 'jp') {
|
|
|
+// return 'https://sellercentral-japan.amazon.com/apps/authorize/consent';
|
|
|
+// } else {
|
|
|
+// //新加坡
|
|
|
+// return 'https://sellercentral.amazon.sg/apps/authorize/consent';
|
|
|
+// }
|
|
|
+// } else {
|
|
|
+// return 'https://sellercentral.amazon.com/apps/authorize/consent';
|
|
|
+// }
|
|
|
+//
|
|
|
+// // https://raycos.com.cn/authorize
|
|
|
+//
|
|
|
+//
|
|
|
+//
|
|
|
+// */
|
|
|
+
|
|
|
+}
|