代码之家  ›  专栏  ›  技术社区  ›  Viktor Fougstedt

Box API使用的初始步骤是否可以自动化?

  •  0
  • Viktor Fougstedt  · 技术社区  · 11 年前

    我是Box的一个企业帐户的管理员,我正在进行一个自动化集成,以根据我们企业的内部目录更新用户的电子邮件地址并设置他们的配额。

    尽管Box API文档似乎针对其他使用场景,但我可以收集到,一旦我获得了access_token/refresh_token对,refresh_taken有效期为60天,在此期间我可以随时获得一个新的。

    我坚信“总会出问题”,我只是想知道是否有任何方法可以自动完成获取access_token/refresh_token对的初始步骤,而不需要浏览器和手动交互。我担心,如果refresh_token由于Box或类似的更新而丢失或无效,这里没有人会记得您是如何手动获取初始令牌对的。

    如果没有办法自动做到这一点,我会接受它,但我不想在没有明确要求知道我没有错过什么的情况下放弃

    2 回复  |  直到 11 年前
        1
  •  0
  •   John Hoerr    11 年前

    [有没有]任何方法可以自动化获取access_token/refresh_token对的初始步骤,这不需要浏览器和手动交互

    不,没有authZ/authN快捷方式。对于能够管理整个企业的客户来说,鉴于他们的权力和影响力,这一比例将翻倍。

    恐怕。。。这里没有人会记得你是如何手动获取初始令牌对的。

    解决这一问题的一种方法可能是实现以下内容:

    1. 创建具有“管理企业”范围的Box应用程序。
    2. 在您的域中创建一个web应用程序 OAuth2 workflow .
    3. 将生成的访问/刷新令牌对存储在您选择的持久性层中
    4. 如果/当由于authZ/authN问题出现问题时,让您的脚本通知组电子邮件帐户,某人需要转到web应用程序并请求新令牌。

    有一些示例web应用程序可帮助您入门。( Python , Asp.NET MVC )

    …Box API文档似乎针对其他使用场景。。。

    Users Events API的一部分,以及 As-User 该功能使整个API企业做好了准备。它很整洁。

        2
  •  0
  •   mrclrchtr    11 年前

    您可以使用如下Web客户端构建一个工作环境:

    import java.io.IOException;
    import java.net.URL;
    import java.util.concurrent.ExecutionException;
    
    import com.gargoylesoftware.htmlunit.BrowserVersion;
    import com.gargoylesoftware.htmlunit.ElementNotFoundException;
    import com.gargoylesoftware.htmlunit.WebClient;
    import com.gargoylesoftware.htmlunit.html.HtmlButton;
    import com.gargoylesoftware.htmlunit.html.HtmlForm;
    import com.gargoylesoftware.htmlunit.html.HtmlPage;
    import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput;
    import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
    import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
    
    public class BoxAuth {
    
    private String key;
    private String email;
    private String password;
    private String redirectUrl;
    private final String AUTH_URL;
    
    public BoxAuth(String key, String email, String password, String redirectUrl) {
        super();
        this.key = key;
        this.email = email;
        this.password = password;
        this.redirectUrl = redirectUrl;
        this.AUTH_URL = "https://www.box.com/api/oauth2/authorize?response_type=code&client_id=" + key + "&redirect_uri=" + this.redirectUrl;
    }
    
    public String authorize() throws IOException, InterruptedException, ExecutionException {
    
        System.out.println("AUTHORIZING: " + AUTH_URL);
    
        final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_17);
    
        HtmlPage loginPage = webClient.getPage(AUTH_URL);
        final HtmlPage grantAccessePage = this.authorizeLogin(loginPage);
        return this.authorizeGrantAccess(grantAccessePage);
    
    }
    
    private HtmlPage authorizeLogin(HtmlPage page) throws IOException {
    
        final HtmlForm loginForm = page.getFormByName("login_form");
        loginForm.getInputByName("password");
        final HtmlTextInput emailField = (HtmlTextInput) loginForm.getInputByName("login");
        emailField.setValueAttribute(this.email);
        final HtmlPasswordInput passwordField = (HtmlPasswordInput) loginForm.getInputByName("password");
        passwordField.setValueAttribute(this.password);
        final HtmlSubmitInput loginButton = loginForm.getInputByName("login_submit");
    
        final HtmlPage result = loginButton.click();
        try {
            final HtmlForm test = result.getFormByName("login_form");
            throw new Exception("BoxAPI: Wrong login data!!!");
        } catch (ElementNotFoundException e) {
        }
    
        return result;
    }
    
    private String authorizeGrantAccess(HtmlPage grantAccessePage) throws IOException, InterruptedException, ExecutionException {
        final HtmlForm grantAccessForm = grantAccessePage.getHtmlElementById("consent_form");
        final HtmlButton grantAccess = grantAccessForm.getButtonByName("consent_accept");
    
        final HtmlPage codePage = grantAccess.click();
    
        URL url = codePage.getUrl();
        String result = "";
    
        if (url.toString().contains("&code=")) {
            result = url.toString().substring(url.toString().indexOf("&code="));
            result = result.replace("&code=", "");
        }
    
        return result;
    }
    
    }
    

    作为redirect_url,您可以使用类似“ https://app.box.com/services/yourservice "