Microsoft Live Connect for Bing Ads OAuth 2.0 无浏览器

Microsoft Live Connect for Bing Ads OAuth 2.0 without browser

我的总体目标是能够每天自动下载 report using the bing ads API. To do this, I need to authenticate with OAuth(旧的 PasswordAuthentication 方法不起作用,因为我有一个新的 Microsoft 帐户)。我已经手动通过 "Authorization Code Grant Flow" 并成功授权了自己。问题是:

Here's an example desktop app using OAuth

有人知道吗

解决方案:

如@eric urban 所述,只需手动授权一次。之后,刷新令牌就可以了。 (仅查看​​示例桌面应用程序并不十分明显!)

我写了一个 class 来处理所有 OAuth 内容并将刷新令牌保存到文件中

public class OAuthRefreshToken {
    private static String refreshTokenFileName = "./bingAdsRefreshToken.txt";
    private static String ClientId = "XXXXX";
    private final OAuthDesktopMobileAuthCodeGrant oAuthDesktopMobileAuthCodeGrant = new OAuthDesktopMobileAuthCodeGrant(ClientId);
    private String refreshToken;

    public OAuthRefreshToken() {
        oAuthDesktopMobileAuthCodeGrant.setNewTokensListener(new NewOAuthTokensReceivedListener() {
            @Override
            public void onNewOAuthTokensReceived(OAuthTokens newTokens) {
                String refreshTime = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                        .format(new java.util.Date());
                refreshToken = newTokens.getRefreshToken();
                System.out.printf("Token refresh time: %s\n", refreshTime);

                writeRefreshTokenToFile();
            }
        });

        getRefreshTokenFromFile();
        refreshAccessToken();
    }

    public OAuthRefreshToken(String refreshToken) {
        this.refreshToken = refreshToken;
        writeRefreshTokenToFile();
    }

    public OAuthDesktopMobileAuthCodeGrant getoAuthDesktopMobileAuthCodeGrant() {
        return oAuthDesktopMobileAuthCodeGrant;
    }

    private void refreshAccessToken(){
        oAuthDesktopMobileAuthCodeGrant.requestAccessAndRefreshTokens(refreshToken);
    }

    private void getRefreshTokenFromFile(){
        try {
            refreshToken = readFile(refreshTokenFileName, Charset.defaultCharset());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String readFile(String path, Charset encoding)
            throws IOException
    {
        byte[] encoded = Files.readAllBytes(Paths.get(path));
        return new String(encoded, encoding);
    }

    private void writeRefreshTokenToFile(){
        File refreshTokenFile = new File(refreshTokenFileName);
        try {
            FileWriter f2 = new FileWriter(refreshTokenFile);
            f2.write(refreshToken);
            f2.close();
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }

        System.out.printf("New refresh token: %s\n", refreshToken);
        System.out.printf("Stored Safely in: %s\n", refreshTokenFileName);
    }

}

在您的应用中使用它,例如:

final OAuthRefreshToken oAuthRefreshToken = new OAuthRefreshToken();
final OAuthDesktopMobileAuthCodeGrant oAuthDesktopMobileAuthCodeGrant = oAuthRefreshToken.getoAuthDesktopMobileAuthCodeGrant();

您说得对,需要事先(一次)征得用户同意。此后,您可以使用刷新令牌请求额外的访问令牌,而无需用户交互。有关使用 Bing 广告 Java SDK 的授权码授予流程的详细信息,请参阅 Getting Started Using Java with Bing Ads Services。这有帮助吗?

刷新令牌不应很快过期,它们通常是永久性的或持续很长时间。但是,如果您请求太多,这些可以被撤销或无效。我相信当您请求超过 25 个不同的刷新令牌时,它们较旧的开始变得无效。