2
我有一些簡單的代碼,用於學習使用路標庫進行OAuth身份驗證。我試圖使用java.util.prefs.Preferences
將請求令牌,令牌機密和引腳保存爲Java首選項。將數據放入首選項可以正常工作(我檢查文件是否已創建並存儲了信息)。但是,在我將put()
的數據立即寫入Preferences文件後,我試着檢查節點是否存在,並且我總是得到false
。java.util.prefs.Preferences.put()工作,但Preferences.nodeExists()總是返回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());
}
}
似乎工作,但我不明白爲什麼'nodeExists(...)'返回false,即使'get(key,null)!= null'。 – 2011-05-15 22:41:43
@Marus - 如果我們說一個節點類似於一個目錄,一個鍵值就像一個文件。所以,如果你把一個文件放進去,那裏沒有'nodeExist'目錄,只是文件存在'get'。 – Ishtar 2011-05-15 22:59:12
對@Ishtar +1,以便對偏好的層次結構進行完美的說明。 – Bozho 2011-05-16 06:54:43