NutzCN Logo
问答 请教个java基础问题,对象初始化为null
发布于 2012天前 作者 wx_vf1cjf7qpjbdmbegllhd 1772 次浏览 复制 上一个帖子 下一个帖子
标签:

OAuth2Api oauth2Api = new OAuth2Api();
oauth2Api.generateUserAuthorizationUrl(Environment.PRODUCTION, Globals.scopes, Optional.of("dev"));
debug跟踪
控制台中显示对象为空:不明白
oauth2Api={ OAuth2Api@6079}
No fields to display

OAth2Api 部分代码:
public class OAuth2Api {

private static final Logger logger = LoggerFactory.getLogger(OAuth2Api.class);
public static final String CRED_SEPERATOR = ":";
private static Map<Environment, OAuth2Api.TimedCacheValue> appAccessTokenMap = new ConcurrentHashMap();

public OAuth2Api() {
}

}

4 回复

你理解的对象为空,是对象的属性全部为null/没有属性?

我理解是没有做初始化,执行这个oauth2Api.generateUserAuthorizationUrl()方法抛出空指针异常。我没想不明白为什么会抛出空指针异常

OAuth2Api 是什么东西??? 不是你自己写的吧, 看文档吧

OAuth2Api 是 Java类
代码:

public class OAuth2Api {

private static final Logger logger = LoggerFactory.getLogger(OAuth2Api.class);
public static final String CRED_SEPERATOR = ":";
private static Map<Environment, OAuth2Api.TimedCacheValue> appAccessTokenMap = new ConcurrentHashMap();

public OAuth2Api() {
}

public OAuthResponse getApplicationToken(Environment environment, List<String> 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<String> scopes, Optional<String> 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();
}
添加回复
请先登陆
回到顶部