I am implementing FB in one of my app.I am using jar 0.8.25. Its working fine on all simulators from 5 to 7.1.And for devices works only for OS 5 and 6 but not working on device 7 and 7.1.For OS 7 after log in success it remains on FB page it doesn't redirect back. and when i press back button, i get error encountered unable to refresh access token with try again button.
When analyzing on console it never finds access token single time for OS 7.while for 5 and 6 its working perfectly.
Please tell what may cause the issue.
Thanks,
This isn't a solution to your specific problem. I mentioned in the comments that I'm using an interface. So I'm posting here as its too much for the comment section. It is also not the COMPLETE solution, you will need to handle the flow and expired tokens, this is just to show you the logic of how I did this.
For my interface I open a browserfield to the Oauth url:
https://www.facebook.com/dialog/oauth?client_id=<APP_ID>&response_type=token&redirect_uri=http://www.facebook.com/connect/login_success.html&scope=publish_actions
And I add a listener to this browser to listen for the redirects after login. Once you have the access token, you should persist it and close the browserfield.
private class OAuthScreen extends MainScreen
{
BrowserField browser_field;
LoadingDialog loading_dialog;
public OAuthScreen(final Command task)
{
super(VERTICAL_SCROLL | HORIZONTAL_SCROLL);
BrowserFieldConfig browserConfig = new BrowserFieldConfig();
browserConfig.setProperty(BrowserFieldConfig.VIEWPORT_WIDTH, new Integer(Display.getWidth()));
browser_field = new BrowserField(browserConfig);
browser_field.addListener(new BrowserFieldListener()
{
public void documentCreated(BrowserField browserField, ScriptEngine scriptEngine, Document document) throws Exception
{
int index = browserField.getDocumentUrl().indexOf("#access_token=");
if (index == -1)
{
super.documentCreated(browserField, scriptEngine, document);
}
else
{
access_token = browserField.getDocumentUrl().substring(index + "#access_token=".length(), browserField.getDocumentUrl().indexOf("&"));
PersistentObject store = PersistentStore.getPersistentObject(STORE_KEY);
FacebookTokens store_tokens = new FacebookTokens();
store_tokens.access_token = access_token;
store.setContents(store_tokens);
store.commit();
if (task != null) task.execute();
OAuthScreen.this.close();
}
}
public void documentLoaded(BrowserField browserField, Document document) throws Exception
{
super.documentLoaded(browserField, document);
loading_dialog.close();
}
});
// whatever loading dialog you want, this sometimes takes a while to open
loading_dialog = LoadingDialog.push(loading_field);
add(browser_field);
browser_field.requestContent("https://www.facebook.com/dialog/oauth?client_id="+APP_ID+"&response_type=token&redirect_uri=http://www.facebook.com/connect/login_success.html&scope=publish_actions");
}
}
The callback task is just for if I want to perform a call directly after login.
Now just perform API calls as you need them. API methods here https://developers.facebook.com/docs/graph-api/reference/v2.0/
Methods that require the access token, should have it appended to the url such as, https://graph.facebook.com/me/feed?access_token=" + access_token
Be aware that clearing your access token won't clear the token stored in the browser field. And will mean that you can't login next time (because the browser is still logged in).
So if you want to logout you need to open this link in a browserfield before clearing your local access token "https://www.facebook.com/logout.php?next=http://www.facebook.com/connect/login_success.html&access_token=" + access_token
Clearing the cookies of the browser should suffice, but I haven't found a way to do this.
Related
I am using keycloak(4.3.0) openid-connect flow. For my client app, I am using:
public class AuthenticationTest {
public static void main(String[] args) {
final String KEYCLOAK_JSON = Constants.KC_CONFIG_JSON_PATH;
InputStream config = Thread.currentThread().getContextClassLoader().getResourceAsStream(KEYCLOAK_JSON);
KeycloakInstalled keycloak;
try {
keycloak = new KeycloakInstalled(config);
config.close();
keycloak.loginDesktop();
AccessToken token = keycloak.getToken();
System.out.printf("UserId: %s%n", token.getSubject());
System.out.printf("Username: %s%n", token.getPreferredUsername());
System.out.printf("Email: %s%n", token.getEmail());
System.out.println(keycloak.getTokenString());
System.in.read();
} catch (IOException | VerificationException | OAuthErrorException | URISyntaxException | HttpFailure
| InterruptedException e) {
e.printStackTrace();
}
}
}
I have referred the example mentioned here: https://gist.github.com/thomasdarimont/ca16080145d226e50628d5696ffb9508
First time I launch the application and login, I get
"Login Successful.You may close this browser window and go back to
your console application."
However, if I launch second instance of my application/ relaunch my application even without closing the previous browser window , still the login page appears instead of you are already logged in message.
(The new login page is appearing in a new tab on the same window as the first login page, so it is not an issue of failure to maintain state across different windows. Also I have tried relaunching the app withing 20 seconds so it does not seem t be an issue of timeout either)
Browser: Firefox, chrome.
Any idea what I am doing wrong. Expectation is that browser must maintain a keycloak session for the same user until closed for at least a few minutes.
NOTE: I did not face this issue in Keycloak 3.4.3. But it started coming on 4.1.0 and 4.3.0
I see follwing difference in cookies on 3.4.3 and 4.x:
3.4.3: (same whther first login or subsequent login)
4.x First time login:
4.x Subsequent login: (an addition KC_RESTART cookie is seen)
I am trying to check whether a page contains next page to sroll. The idea is basic: get the current link, remove the index at the end and then put new index, try to connect to the new link, if it throws IOException that means it does not have next button so that can be scrolled. Here is my code, and the problem it returns false even to the links that Jsoup can connect:
public boolean checkNextButton(String linkToCheck) throws IOException {
boolean containsNextButton = true;
try {
//Here I am trying to connect to a link which is actually available but it still returns false
Document doc = Jsoup.connect(turnToNextPage(linkToCheck)).get();
specific.setPageChangeRate(specific.getPageChangeRate() - 1);
//Set back the counter to the previous one
} catch (IOException e) {
containsNextButton = false;
}
System.out.println("The page contains next button: " + containsNextButton);
return containsNextButton;
}
Solution provided by Pshemo:
It is hard to tell what exactly went wrong without more details.
Are you able to access resource from linkToCheck via browser? Did you need to log-in on that site first (maybe try accessing it via incognito mode to see if it is available for anyone without creating an account).
Other problem could be that some servers allow only specific browsers and they try to recognize them by using user-agent header browsers send, or if server is creating temporary session you may need to pass cookies from previous connection.
I am using the Spotify Android SDK version spotifysdk-1.0.0-beta8.aar. I have a valid OAuth token which I am refreshing each hour successfully. This is how I am getting the Spotify Player Object initially
mPlayer = Spotify.getPlayer(playerConfig, this, new Player.InitializationObserver() {
#Override
public void onInitialized(Player player) {
player.addConnectionStateCallback(JukeSpotDashboardActivity.this);
player.addPlayerNotificationCallback(JukeSpotDashboardActivity.this);
player.login(utils.getPreferenceValue(getString(R.string.spotify_access_token)));
}
#Override
public void onError(Throwable throwable) {
Log.e(JukeSpotDashboardActivity.class.getName(),
"Could not initialize player: " + throwable.getMessage());
}
});
At the end of each hour the access token gets refreshed and stored in the shared preferences. How do you guys suggest I use the new access token in the same mPlayer object so that which ever song is being streamed currently won't get stopped and I won't need to recreate this Spotify player object?
I know there is the
mPlayer.login(accessToken);
function, but it did not work when I got the new accessToken and relogged in. Any idea what I am doing wrong?
Once logged in, your application will be able to stream until the application is stopped, the network connection is lost, or the user's premium account has ended. (There may be other events as well.) This means that you don't need to update the Player object with the refreshed access token to continue streaming.
Note that if you're making any other requests using an access token such as adding a track to a playlist, the access token must be updated in whatever tool you're using to perform the request. For example, if you're using kaaes' excellent Spotify Web API client for Android, you'd need to get a new SpotifyService instance based on the refreshed access token.
What I want to do:
I am trying to make a simple program that posts 5-10 statuses, at a time, on a page's wall. The post to the page will have to be done under the name of the page.
I've read tons of badly written Facebook Developers documentation and I'm reaching the point of confusion where I don't even know what questions to ask. So her I am.
My code so far:
I manually got the Page Access token manually, by this method:
Go to https://developers.facebook.com/tools/explorer
At the GET request form, down there, fill in me/accounts
You'll get a Javascript representation of your basic user data. Find the page you want.
Note the access_token and id fields, we're going to use them in the code below.
Thus, after getting the page Access token manually (And the ID of the page, of course)
import com.restfb.DefaultFacebookClient;
import com.restfb.FacebookClient;
import com.restfb.Parameter;
import com.restfb.exception.FacebookException;
import com.restfb.types.FacebookType;
import com.restfb.types.Page;
import com.restfb.types.User;
/**
*
* #author dsfounis
*/
public class FacebookConnector {
/* Variables */
private final String pageAccessToken = "GOT_THIS_FROM_THE_METHOD_ABOVE";
private final String pageID = "THIS_TOO";
private FacebookClient fbClient;
private User myuser = null; //Store references to myr user and page
private Page mypage = null; //for later use. In this question's context, these
//references are useless.
private int counter = 0;
public FacebookConnector() {
try {
fbClient = new DefaultFacebookClient(pageAccessToken);
myuser = fbClient.fetchObject("me", User.class);
mypage = fbClient.fetchObject(pageID, Page.class);
counter = 0;
} catch (FacebookException ex) { //So that you can see what went wrong
ex.printStackTrace(System.err); //in case you did anything incorrectly
}
}
public void makeTestPost() {
fbClient.publish(pageID + "/feed", FacebookType.class, Parameter.with("message", Integer.toString(counter) + ": Hello, facebook World!"));
counter++;
}
}
The problem:
The code above works. The thing is, it works temporarily. The page access token that I get has an expiration time of one hour, and I need to manually go through the process of obtaining it, every time that I run the program. What is the point of automating a process if I keep some aspects of it manual?
So I have to ask you: Can I do the process above programmatically, and obtain a fresh page access token at program launch?
Can I, maybe, use a better API to do something as simple as just post a couple of things on a Page's wall, every day?
My application is a console one, and I would like to stay away from implementing needless Logins, even though if you tell me that it is needed, it's going to be a bother I'll have to go through.
As a note: I've got the application registered in Facebook Developers, albeit only as a basic app. To get more permissions, I need to show proof of Facebook Login implementation, and as I say in the title, it's something I'll have to avoid.
There is no automatic process to obtain an access token. If there was, it will defeat the whole purpose of the OAuth flow. For pet projects and tests it's okay to use the Graph API Explorer but for public applications involving users it is mandatory that the user manually selects the login dialog.
Under your current scenario you can extend the user token using the method mentioned here https://developers.facebook.com/docs/roadmap/completed-changes/offline-access-removal/
Scenario 5: Page Access Tokens
When a user grants an app the manage_pages permission, the app is able
to obtain page access tokens for pages that the user administers by
querying the [User ID]/accounts Graph API endpoint. With the migration
enabled, when using a short-lived user access token to query this
endpoint, the page access tokens obtained are short-lived as well.
Exchange the short-lived user access token for a long-lived access
token using the endpoint and steps explained earlier.
https://graph.facebook.com/oauth/access_token?
client_id=APP_ID&
client_secret=APP_SECRET&
grant_type=fb_exchange_token&
fb_exchange_token=EXISTING_ACCESS_TOKEN
By using a
long-lived user access token, querying the [User ID]/accounts endpoint
will now provide page access tokens that do not expire for pages that
a user manages. This will also apply when querying with a non-expiring
user access token obtained through the deprecated offline_access
permission.
A simple program used only by the owner of the application does not need approval from Facebook.
e.g. https://www.facebook.com/phwdbot
My Requirement is to sent a mail to the concerned users when a record is created. The mail contains a link to our system that lets the user interacting with the system without login. The link expires after a certain time. The mailing is done using javax.mail.
How can I expire the link?
Use Timer#schedule(int):
// Schedule the timer to run once in 1 minute.
new Timer()
{
#Override
public void run()
{
if(remove)
{
// Either remove it
anchor.removeFromParent();
}
else
{
// Or disable it
anchor.setEnabled(false);
}
}
}.schedule(60 * 1000);
I would generate a key/ID that you add to the link and also store to a database. With filters (web.xml) you can check if the URL (ID) is still valid and pass it on to the desired page.
If you provide us with more details, we can give you a more detailed answer.