protected synchronized void getAccessToken ( AipClientConfiguration config ) { if ( ! needAuth ( ) ) { if ( LOGGER . isDebugEnabled ( ) ) { LOGGER . debug ( String . format ( "app[%s] no need to auth" , this . appId ) ) ; } return ; } JSONObject res = DevAuth . oauth ( aipKey , aipToken , config ) ; if ( res == null ) { LOGGER . warn ( "oauth get null response" ) ; return ; } if ( ! res . isNull ( "access_token" ) ) { // openAPI认证成功 state . transfer ( true ) ; accessToken = res . getString ( "access_token" ) ; LOGGER . info ( "get access_token success. current state: " + state . toString ( ) ) ; Integer expireSec = res . getInt ( "expires_in" ) ; Calendar c = Calendar . getInstance ( ) ; c . add ( Calendar . SECOND , expireSec ) ; expireDate = c ; // isBceKey.set(false); // 验证接口权限 String [ ] scope = res . getString ( "scope" ) . split ( " " ) ; boolean hasRight = false ; for ( String str : scope ) { if ( AipClientConst . AI_ACCESS_RIGHT . contains ( str ) ) { // 权限验证通过 hasRight = true ; break ; } } state . transfer ( hasRight ) ; if ( LOGGER . isDebugEnabled ( ) ) { LOGGER . debug ( "current state after check priviledge: " + state . toString ( ) ) ; } } else if ( ! res . isNull ( "error_code" ) ) { state . transfer ( false ) ; LOGGER . warn ( "oauth get error, current state: " + state . toString ( ) ) ; } }
get OAuth access token synchronized function