java new 对象 null_请教个java基础问题,对象初始化为null

OAuth2Api 是 Java类

代码:

public class OAuth2Api {

private static final Logger logger = LoggerFactory.getLogger(OAuth2Api.class);

public static final String CRED_SEPERATOR = ":";

private static Map appAccessTokenMap = new ConcurrentHashMap();

public OAuth2Api() {

}

public OAuthResponse getApplicationToken(Environment environment, List scopes) throws IOException {

OAuth2Api.TimedCacheValue appAccessToken = (OAuth2Api.TimedCacheValue)appAccessTokenMap.get(environment);

if (appAccessToken != null && appAccessToken.getValue() != null) {

logger.debug("application access token returned from cache");

return appAccessToken.getValue();

} else {

OkHttpClient client = new OkHttpClient();

String scope = (String) OAuth2Util.buildScopeForRequest(scopes).orElse("");

CredentialUtil.Credentials credentials = CredentialUtil.getCredentials(environment);

String requestData = String.format("grant_type=client_credentials&scope=%s", scope);

RequestBody requestBody = RequestBody.create(MediaType.parse("application/x-www-form-urlencoded"), requestData);

Request request = (new Request.Builder()).url(environment.getApiEndpoint()).header("Authorization", this.buildAuthorization(credentials)).header("Content-Type", "application/x-www-form-urlencoded").post(requestBody).build();

Response response = client.newCall(request).execute();

if (response.isSuccessful()) {

logger.debug("Network call to generate new token is successfull");

OAuthResponse oAuthResponse = OAuth2Util.parseApplicationToken(response.body().string());

AccessToken accessToken = (AccessToken)oAuthResponse.getAccessToken().get();

appAccessToken = new OAuth2Api.TimedCacheValue(oAuthResponse, new DateTime(accessToken.getExpiresOn()));

appAccessTokenMap.put(environment, appAccessToken);

return oAuthResponse;

} else {

return OAuth2Util.handleError(response);

}

}

}

private String buildAuthorization(CredentialUtil.Credentials credentials) {

StringBuilder sb = new StringBuilder();

sb.append(credentials.get(CredentialUtil.CredentialType.APP_ID)).append(":").append(credentials.get(CredentialUtil.CredentialType.CERT_ID));

byte[] encodeBytes = Base64.getEncoder().encode(sb.toString().getBytes());

return "Basic " + new String(encodeBytes);

}

public String generateUserAuthorizationUrl(Environment environment, List scopes, Optional state) {

StringBuilder sb = new StringBuilder();

CredentialUtil.Credentials credentials = CredentialUtil.getCredentials(environment);

String scope = (String)OAuth2Util.buildScopeForRequest(scopes).orElse("");

sb.append(environment.getWebEndpoint()).append("?");

sb.append("client_id=").append(credentials.get(CredentialUtil.CredentialType.APP_ID)).append("&");

sb.append("response_type=code").append("&");

sb.append("redirect_uri=").append(credentials.get(CredentialUtil.CredentialType.REDIRECT_URI)).append("&");

sb.append("scope=").append(scope).append("&");

if (state.isPresent()) {

sb.append("state=").append((String)state.get());

}

logger.debug("authorize_url=" + sb.toString());

return sb.toString();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值