java.util.prefs.Preferences.put() 有效,但 Preferences.nodeExists() 始终返回 false
我有一些简单的代码,用于使用路标库学习 OAuth 身份验证。我正在尝试使用 java.util.prefs.Preferences 将请求令牌、令牌机密和 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());
}
}
I have some simple code I am using to learn OAuth authentication using the signpost library. I am trying to save the request token, the token secret and the pin as Java preferences, using java.util.prefs.Preferences
. Putting the data into the preferences works fine (I check that the file is created and the information stored). However, immediatelly after I put()
the data into the Preferences file, I try to check if the node exists, and I always get false
.
I also tried just re-running the code, and checking for the node's existence before I try to save the data again, and I still get false.
What am I doing incorrectly?
Here is the code:
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());
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
put(..)
的相反操作是get(..)
。因此:如果在指定的键下没有找到任何内容,
get(..)
方法将返回第二个参数。nodeExists()
与首选项的层次结构相关,可以在使用prefs.node(..)
创建节点时使用。但我认为你似乎需要一个扁平的结构,所以你不需要层次结构。The opposite operation of
put(..)
isget(..)
. So:the
get(..)
method returns the 2nd argument if there is nothing found under the specified key.the
nodeExists()
is related to the hierarchical structure of preferences, and can be used when you've usedprefs.node(..)
to create a node. But you seem to need a flat structure, so you don't need hierarchies, I think.