gpt4 book ai didi

java.util.prefs.Preferences.put() 有效,但 Preferences.nodeExists() 总是返回 false

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:36:00 25 4
gpt4 key购买 nike

我有一些简单的代码,我正在使用它来学习使用路标库的 OAuth 身份验证。我正在尝试使用 java.util.prefs.Preferences 将请求 token 、 token secret 和 pin 保存为 Java 首选项。将数据放入首选项中效果很好(我检查文件是否已创建并存储了信息)。然而,在我将数据 put() 到 Preferences 文件后,我立即尝试检查该节点是否存在,但我总是得到 false

我也尝试过重新运行代码,并在尝试再次保存数据之前检查节点是否存在,但我仍然得到错误。

我做错了什么?

代码如下:

package com.example;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.prefs.Preferences;

import oauth.signpost.OAuth;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthConsumer;
import oauth.signpost.basic.DefaultOAuthProvider;

public class ReadMain {

public static void main(String[] args) throws Exception {

OAuthConsumer consumer =
new DefaultOAuthConsumer(
"??",
"??");

OAuthProvider provider =
new DefaultOAuthProvider(
"https://www.readability.com/api/rest/v1/oauth/request_token/",
"https://www.readability.com/api/rest/v1/oauth/access_token/",
"https://www.readability.com/api/rest/v1/oauth/authorize/");

System.out.println("Fetching request token from Readability...");

Preferences prefs = Preferences.userNodeForPackage(ReadMain.class);

final String PREF_TokenSecret = "com/example/TS";
final String PREF_RequestToken = "com/example/RT";
final String PREF_ReadabilityPin = "com/example/RP";

boolean exists =
prefs.nodeExists( PREF_TokenSecret ) &&
prefs.nodeExists( PREF_RequestToken ) &&
prefs.nodeExists( PREF_ReadabilityPin );

if ( prefs.nodeExists( PREF_TokenSecret ) )
{
System.out.println("Token secret exists!");
}

if ( prefs.nodeExists( PREF_RequestToken ) )
{
System.out.println("Request token exists!");
}

if ( prefs.nodeExists( PREF_ReadabilityPin ) )
{
System.out.println("Readability pin exists!");
}

String pin;

if ( exists )
{
consumer.setTokenWithSecret(
prefs.get(PREF_RequestToken,""),
prefs.get(PREF_TokenSecret,"") );
pin = prefs.get(PREF_ReadabilityPin,"");
}
else
{
// we do not support callbacks, thus pass OOB
String authUrl = provider.retrieveRequestToken(consumer, OAuth.OUT_OF_BAND);

System.out.println( "Request token: " + consumer.getToken() );
System.out.println( "Token secret: " + consumer.getTokenSecret() );

prefs.put( PREF_RequestToken, consumer.getToken() );
prefs.put( PREF_TokenSecret, consumer.getTokenSecret() );

System.out.println( "Now visit:\n" + authUrl + "\n... and grant this app authorization" );
System.out.println( "Enter the PIN code and hit ENTER when you're done:" );

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
pin = br.readLine();

prefs.put(PREF_ReadabilityPin, pin);

if ( prefs.nodeExists( PREF_TokenSecret ) )
{
System.out.println("Token secret exists!");
}

if ( prefs.nodeExists( PREF_RequestToken ) )
{
System.out.println("Request token exists!");
}

if ( prefs.nodeExists( PREF_ReadabilityPin ) )
{
System.out.println("Readability pin exists!");
}
}

System.out.println("Fetching access token from Readability...");
provider.retrieveAccessToken(consumer, pin);

System.out.println("Access token: " + consumer.getToken());
System.out.println("Token secret: " + consumer.getTokenSecret());

URL url = new URL("https://www.readability.com/api/rest/v1/bookmarks?user=marcusps&archive=1");
HttpURLConnection request = (HttpURLConnection) url.openConnection();

consumer.sign(request);

System.out.println("Sending request to Readability...");
request.connect();

System.out.println("Response: " + request.getResponseCode() + " "
+ request.getResponseMessage());
}
}

最佳答案

put(..)的相反操作是get(..)。所以:

boolean exists = prefs.get(key, null) != null;

get(..) 方法在指定键下没有找到任何内容时返回第二个参数。

nodeExists() 与偏好的层次结构相关,可以在您使用 prefs.node(..) 创建节点时使用.但你似乎需要一个平面结构,所以我认为你不需要层次结构。

关于java.util.prefs.Preferences.put() 有效,但 Preferences.nodeExists() 总是返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6011605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com