api
package com.rcplatform.athena.coin.api;
import com.rcplatform.athena.coin.models.data.GooglePlayOauthResponse;
import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;
/**
* Created by Yang Peng on 2017/5/11.
*/
public interface GooglePlayOauthApi {
/**
* grant_type=authorization_code
* code=<the code from the previous step>
* client_id=<the client ID token created in the APIs Console>
* client_secret=<the client secret corresponding to the client ID>
* redirect_uri=<the URI registered with the client ID>
*
* @return
*/
@FormUrlEncoded
@POST("https://accounts.google.com/o/oauth2/token")
Call<GooglePlayOauthResponse> oauth(@Field("grant_type") String grantType, @Field("code") String code,
@Field("client_id") String clientId, @Field("client_secret") String clientSecret,
@Field("redirect_uri") String redirectUri);
/**
* Using the refresh token
* <p>
* Each access token is only valid for a short time. Once the current access token expires, the server will need to use the refresh token to get a new one. To do this, send a POST request to https://accounts.google.com/o/oauth2/token with the following fields set:
* <p>
* grant_type=refresh_token
* client_id=<the client ID token created in the APIs Console>
* client_secret=<the client secret corresponding to the client ID>
* refresh_token=<the refresh token from the previous step>
*
* @param grantType
* @param refreshToken
* @param clientId
* @param clientSecret
* @return
*/
@FormUrlEncoded
@POST("https://accounts.google.com/o/oauth2/token")
Call<GooglePlayOauthResponse> refreshToken(@Field("grant_type") String grantType, @Field("refresh_token") String refreshToken,
@Field("client_id") String clientId, @Field("client_secret") String clientSecret);
}
请求类
public class GooglePlayPaymentService {
private GooglePlayOauthApi googlePlayOauthApi;
private GooglePlayPaymentApi googlePlayPaymentApi;
public GooglePlayPaymentService(String baseUrl) {
this.baseUrl = baseUrl;
Retrofit retrofit = new RetrofitClient(baseUrl).getRetrofit();
googlePlayPaymentApi = retrofit.create(GooglePlayPaymentApi.class);
googlePlayOauthApi = retrofit.create(GooglePlayOauthApi.class);
}
调用
@Retryable(value = HttpErrorException.class, backoff = @Backoff(value = 10000))
public GooglePlayProductResponse productVerify(String packageName, String productId, String token, String accessToken) throws TokenExpireException, HttpErrorException {
logger.debug("发起google play 消耗购买验证");
Call<GooglePlayProductResponse> call =
googlePlayPaymentApi.getProduct(packageName, productId, token, accessToken);
Response<GooglePlayProductResponse> response;
try {
response = call.execute();
if (response.code() == 401) {
logger.error("当前token expired");
throw new TokenExpireException();
}
} catch (IOException e) {
logger.error("we have some trouble packageName{},productId{},token{},accessToken{}", packageName, productId, token, accessToken);
throw new HttpErrorException(e.getMessage());
}
return response.body();
}
private <T> T getResponse(Call<T> t) throws TokenExpireException, HttpErrorException {
try {
Response<T> response = t.execute();
if (response.code() == 401) {
throw new TokenExpireException();
}
return response.body();
} catch (IOException e) {
throw new HttpErrorException();
}
}
注入
@Configuration
@EnableConfigurationProperties(PaymentProperties.class)
public class PaymentAutoConfiguration {
private PaymentProperties paymentProperties;
public PaymentAutoConfiguration(PaymentProperties paymentProperties) {
this.paymentProperties = paymentProperties;
}
@Bean
@Primary
public AppStorePaymentService appStorePaymentService() {
return new AppStorePaymentService(paymentProperties.getAppStorePassword(), paymentProperties.getAppStoreUrl());
}
@Bean
@ConditionalOnMissingBean
public GooglePlayPaymentService googlePlayPaymentService() {
return new GooglePlayPaymentService(paymentProperties.getGooglePlayUrl());
}
@Bean
public AppStorePaymentService appStoreSandboxPaymentService() {
return new AppStorePaymentService(paymentProperties.getAppStoreSandboxUrl());
}
}
retrofit
restful的http网络请求框架,本质okhttp完成,仅负责网络请求接口的封装
https://www.jianshu.com/p/865e9ae667a0