获取JRaw的空访问令牌

时间:2018-10-02 02:28:14

标签: java kotlin reddit

根据我的理解,运行这段代码时,Jraw中的OAuthHelper应该自动生成一个访问令牌,但事实并非如此。我究竟做错了什么?我在应用程序平台字段中输入了一个随机值。这有关系吗?

public static void main(String[] args) {
    UserAgent userAgent = new UserAgent(
            "appPlatform",
           "appName",
            "version",
            "userName");

    Credentials credentials = Credentials.script(
           "userName",
            "password",
            "clientId",
            "clientSecret"
    );

    NetworkAdapter adapter = new OkHttpNetworkAdapter(userAgent);
    RedditClient redditClient = OAuthHelper.automatic(adapter, credentials);

    // frontPage() returns a Paginator.Builder
    DefaultPaginator<Submission> frontPage = redditClient.frontPage()
            .sorting(SubredditSort.TOP)
            .timePeriod(TimePeriod.DAY)
            .limit(30)
            .build();

    Listing<Submission> submissions = frontPage.next();
    for (Submission s : submissions) {
        System.out.println(s.getTitle());
    }
}

这是堆栈跟踪:

  

线程“ main”中的异常java.lang.NullPointerException:空   的accessToken   net.dean.jraw.models.internal。$ AutoValue_OAuthDataJson。($ AutoValue_OAuthDataJson.java:23)     在   net.dean.jraw.models.internal.AutoValue_OAuthDataJson。(AutoValue_OAuthDataJson.java:15)     在   net.dean.jraw.models.internal.AutoValue_OAuthDataJson $ MoshiJsonAdapter.fromJson(AutoValue_OAuthDataJson.java:64)     在   net.dean.jraw.models.internal.AutoValue_OAuthDataJson $ MoshiJsonAdapter.fromJson(AutoValue_OAuthDataJson.java:18)     在com.squareup.moshi.JsonAdapter.fromJson(JsonAdapter.java:41)   net.dean.jraw.http.HttpResponse.deserializeWith(HttpResponse.kt:49)     在   net.dean.jraw.oauth.OAuthHelper.scriptOAuthData $ lib(OAuthHelper.kt:96)     在net.dean.jraw.oauth.OAuthHelper.automatic(OAuthHelper.kt:32)处   net.dean.jraw.oauth.OAuthHelper.automatic $ default(OAuthHelper.kt:27)     在net.dean.jraw.oauth.OAuthHelper.automatic(OAuthHelper.kt)处   com.signalscanner.reddit.RedditAuthenticator.main(RedditAuthenticator.java:34)

应该注意RedditAuthenticator.java:34是这一行:

RedditClient redditClient = OAuthHelper.automatic(适配器,凭证);

1 个答案:

答案 0 :(得分:1)

我相信我已经找到了原因。在Reddit中创建应用时,需要在此部分中选择脚本:

enter image description here

我正在调试JRAW,以查看它的作用,并在构建发送到Reddit的HTTP请求时,正在为您的应用程序类型添加一个参数。我不知道您是否可以通过某种方式将JRAW配置为使用其他任何类型,但默认使用的是“脚本”

编辑:有关JRAW的不同身份验证方案的更多信息,可以在这里找到-https://mattbdean.gitbooks.io/jraw/content/v/v1.1.0/oauth2.html