首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何将代理添加到box java sdk中的BoxApiConnection?

如何将代理添加到box java sdk中的BoxApiConnection?
EN

Stack Overflow用户
提问于 2015-09-25 17:49:09
回答 2查看 1.1K关注 0票数 0

我需要在方框连接中设置代理。但不能得到任何参考代码到use.Can有人给我解决方案吗?

EN

回答 2

Stack Overflow用户

发布于 2015-10-19 15:08:58

BoxAPIConnection接口=新代码(BoxAPIConnection);

代码语言:javascript
复制
    Proxy proxy = null;
    try {
        proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(InetAddress.getByName("proxy URL"), PortNo));
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
    api.setProxy(proxy);
票数 2
EN

Stack Overflow用户

发布于 2019-05-27 00:53:17

我在没有代理的BoxDeveloperEditionAPIConnection上也遇到了同样的问题。我创建了下面的代码来克服这个问题,它提供了在到期时间后自动刷新的令牌:

代码语言:javascript
复制
import com.box.sdk.BoxConfig;
import com.box.sdk.BoxDeveloperEditionAPIConnection;
import com.box.sdk.DeveloperEditionEntityType;
import com.box.sdk.IAccessTokenCache;
import com.eclipsesource.json.JsonObject;

import java.net.Proxy;

public class MyBoxAPIConnection extends BoxDeveloperEditionAPIConnection {

private IAccessTokenCache accessTokenCache;
private String entityID;
private DeveloperEditionEntityType entityType;

public MyBoxAPIConnection(BoxConfig boxConfig, IAccessTokenCache accessTokenCache, Proxy proxy) {
    this(boxConfig.getEnterpriseId(), DeveloperEditionEntityType.ENTERPRISE, boxConfig, accessTokenCache);
    this.setProxy(proxy);
    this.tryRestoreUsingAccessTokenCache();
}

public MyBoxAPIConnection(String entityId, DeveloperEditionEntityType entityType, BoxConfig boxConfig,
        IAccessTokenCache accessTokenCache) {
    super(entityId, entityType, boxConfig, accessTokenCache);
}

private void tryRestoreUsingAccessTokenCache() {
    if (this.accessTokenCache == null) {
        // no cache specified so force authentication
        this.authenticate();
    } else {
        String cachedTokenInfo = this.accessTokenCache.get(this.getAccessTokenCacheKey());
        if (cachedTokenInfo == null) {
            // not found; probably first time for this client config so authenticate; info will then be cached
            this.authenticate();
        } else {
            // pull access token cache info; authentication will occur as needed (if token is expired)
            JsonObject json = JsonObject.readFrom(cachedTokenInfo);
            this.setAccessToken(json.get("accessToken").asString());
            this.setLastRefresh(json.get("lastRefresh").asLong());
            this.setExpires(json.get("expires").asLong());
        }
    }
}

private String getAccessTokenCacheKey() {
    return String.format("/%s/%s/%s/%s", this.getUserAgent(), this.getClientID(), this.entityType.toString(),
            this.entityID);
}

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32779453

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档