Spotify Web API Authorization Error - java

I am writing an application in Java that works with the Spotify Web API to get the album artwork of the currently playing album (and maybe other stuff in the future, hence the long list of scopes). Per Spotify's guide, I have to use callbacks in order to get the access token. However, when using the authorization link, Spotify gives me the following intensely helpful and insightful error message.
Spotify Error Message
The code I am using to call open a window is
if(Desktop.isDesktopSupported())
{
String url = "https://accounts.spotify.com/authorize/";
url += "client_id="+SpotifyClientID;
url += "&response_type=code";
url += "&redirect_uri=http%3A%2F%2Flocalhost%3A8888%2Fcallback%2F";
url += "&state="+state;
url += "&scope=playlist-read-private%20playlist-read-collaborative%20user-library-read%20user-read-private%20user-read-playback-state%20user-modify-playback-state%20user-read-currently-playing";
Desktop.getDesktop().browse(new URI(url));
}
Similar questions have been asked, and their issue was their callback URL was not whitelisted; however, I went to the Spotify Dashboard and made SURE http://localhost:8888/callback/ was whitelisted. I've tried using 'http://localhost:8888/callback/' directly in the URL, and I've also tried HTML escaping it, so that it becomes 'http%3A%2F%2Flocalhost%3A8888%2Fcallback%2F' as shown in the code above. Can anyone give me an insight as to why the error message appears instead of the login page?

Figured it out myself. Turns out, I am awesome at links. /s Changed the last '/' in "https://accounts.spotify.com/authorize/" into a '?' so that it would actually receive the parameters I was passing it and it worked perfectly.

Related

Plaid Quickstart Issues (Java)

I am trying to implement Plaid using the sample code provided on the Java Quickstart [sandbox] and am getting issues when I show the Plaid Dialog (javascript). I am able to successfully get a link_token, but I'm never able to show the dialog. It spins for a brief second, then shows me:
oauth uri does not contain a valid oauth_state_id query parameter. Request ID: DBoT92FCo8AORay
I have tried this with an empty redirectUri, as well as "http://localhost:8080/plaid_test.html", which is registered in my developer account.
I am a bit stuck and hoping someone can direct me in the right direction. I've tested with both versions 9.10.0 and the latest (11.9.0).
Curiously, I am able to get the Java Quickstart working directly, but ONLY if I leave the .env PLAID_REDIRECT_URI blank. If I put localhost in there, it fails when trying to get the link token.
Does anyone have any suggestions on how to overcome this setup issue?
Thank you!
I got this error (oauth uri does not contain a valid oauth_state_id query parameter) while creating a new test application in Plaid's Sandbox environment.
Important note: My application does not use OAuth.
The problem turned out to be, in my configuration parameters being passed to usePlaidLink, I was including a receivedRedirectUri key-value pair. Removing that key-value pair entirely resolved the issue for me.
In other words, my React component looked something like:
import { usePlaidLink } from 'react-plaid-link';
function PlaidLink(props) {
const onSuccess = React.useCallback((public_token, metadata) => {
// ...
});
const config = {
token: props.linkToken,
receivedRedirectUri: window.location.href,
onSuccess,
};
const { open, ready } = usePlaidLink(config);
// ...
}
Removing the line with the receivedRedirectUri was the solution for me, getting me past the oauth uri does not contain a valid oauth_state_id query parameter error, and getting the Plaid Link UI to appear in my app successfully.
This Plaid article, which has a number of mentions of "OAuth state ID" (as mentioned in the error message), helped point me toward this solution.
The issue may be the location you are trying to use -- unless you have manually modified the ports or other code used by the Quickstart, you should use http://localhost:3000/ as the PLAID_REDIRECT_URI (make sure to add this to your Dashboard as an allowed redirect URI). When I tried this just now on the Java quickstart (non-Docker version) it worked fine.

How to write a Java code to read fields from a website that requires login and uses POST request?

Need some help with fetching some data from a website.
Previously , we had following code in our application and it used to fetch the required data. We just used to read the required fields by forming a URL by passing username , password and search parameter (DEA number). The same URL (with parameters ) could also be hit from browser directly to see the results. It was a simple GET request:
{URL url = new URL(
"http://www.deanumber.com/Websvc/deaWebsvc.asmx/GetQuery?UserName="+getUsername()+"&Password="+getPassword()+"&DEA="
+ deaNumber
+ "&BAC=&BASC=&ExpirationDate=&Company=&Zip=&State=&PI=&MaxRows=");
Document document = parser.parse(url.toExternalForm());
// Ask the document for a list of all <sect1> tags it contains
NodeList sections = document.getElementsByTagName("DEA");
//Followed by a loop code to get each element by using sections.item(index).getFirstChild() etc.
}
Now, the website URL has got changed to following:
https://www.deanumber.com/RelId/33637/ISvars/default/Home.htm
I am able to login to the URL with credentials , go to the search page , enter the DEA number and search. The login page comes as a pop-up once I click 'Login' link on home page. Also, the final result comes as a pop-up. This is a POST request so I am unable to form the complete URL which I could use in my code.
I am not an expert in Web Services , but I think I need a web service URL like the one mentioned in the code above. Not sure how to get that !! Even if I get the URL , I am not sure how to perform the login through Java code and search the DEA number.
Also, it would be great if I could validate the URL manually before using in Java. Let me know if there is any way.
Or, in case there is any alternate approach in Java; kindly suggest.
Thanks in advance.
First of all, the previous approach provided by the website was completely wrong and insecure, because it passes the username and password as querystring parameters in plain text. I think, they would have realized this thing and changed their way of authentication.
Also, it looks like that they have restricted the direct URL based requests from the client applications like yours. For such requests from clients, they have published the web services. Check this link. They also have mentioned the rates for web service request counts.
So, you may need to open a formal communication channel to get authentication and other details to access their web services for this purpose. Depends on what they use for web service client authentication, you may code your client to access the web services.
I hope this helps.

POST String as parameter in Url

I am testing foursquare api. Here is the url.
https://api.foursquare.com/v2/checkins/add?venueId=123&shout=Test&broadcast=public,twitter,facebook&oauth_token=something
the problem is when I add shout="test". It works fine. But "test message" isn't working. I mean test[space]message isn't working.I tried with removing spaces,it worked.But,that can't be solution. How can I handle such thing I mean how can I send post request with shout="test messgae" with space include.
Try using shout="test%20message"
%20 is just HTML code for a space
This is another post found on Stack Overflow that provides more detail Encoding URL Query Parameters

Facebook posting with link param failing on first try

I am trying to post to Facebook using Graph API and one of the parameters on that is link.
Example URL:
https://graph.facebook.com/me/feed?access_token=Xxxx&message=&link=http://something/token/123456B&description=test
Every time the link changes the posting fails the first time. If I retry twice or thrice the call works.
Here is the error I am getting :
{"error":{"message":"Call to a member function getImageInfo() on a non-object","type":"BadMethodCallException"}}
This used to work just fine but suddenly stopped working 2 days ago.
The link always changes for us and such its breaking all the posts.
Did anything change with the API recently with Facebook ? Any help will be highly appreciated
well i fixed...i think...
in the url link, the meta og:image it can't be empty as '' and the link must be absolute. if the link don't have an image, don't put the meta. by the way i update the app at the fb app panel to February 2013 Breaking Changes to activate.
I have the same problem in iOS development, response from server:
"com.facebook.sdk:HTTPStatusCode" = 500;
"com.facebook.sdk:ParsedJSONResponseKey" = {
body = {
error = {
message = "Call to a member function getImageInfo() on a non-object";
type = BadMethodCallException;
};
};
code = 500;
};
How can help? :)
I had the same issue and just managed to fix it.
Was an issue with the og:image element in the page header of the URL.
My image link was relative to the website but I changed it to absolute and all seems to work now.
<meta property="og:image" content="{should be absolute url here}" />
It seems to be up and down today. It is fine sometimes, but most of the time it doesn't work.
This seems to be a Facebook backend error message issue. You should consider reporting this error message and behavior at Facebook Developers Bug Reporting System

Redirect to servlet fails

I have a servlet named EditPhotos which, believe it or not, is used for editing the photos associated with a certain item on a web design I am developing. The URL path to edit a photo is [[SITEROOT]]/EditPhotos/[[ITEMNAME]].
When you go to this path (GET), the page loads fine. You can then click on a 'delete' link that POSTs to the same page, telling it to delete the photo. The servlet receives this delete command properly and successfully deletes the photo. It then sends a redirect back to the first page (GET).
For some reason, this redirect fails. I don't know how or why, but using the HTTPFox plugin for firefox, I see that the POST request receives 0 bytes in response and has the code NS_BINDING_ABORTED.
The code I am using to send the redirect, is the same code I have used throughout the website to send redirects:
response.sendRedirect(Constants.SITE_ROOT + "EditPhotos/" + itemURL);
I have checked the final URL that the redirect sends, and it is definitely correct, but the browser never receives the redirect. Why?
Read the server logs. Do you see IllegalStateException: response already committed with the sendRedirect() call in the trace?
If so, then that means that the redirect failed because the response headers are already been sent. Ensure that you aren't touching the HttpServletResponse at all before calling the sendRedirect(). A redirect namely exist of basically a Location response header with the new URL as value.
If not, then you're probably handling the request using JavaScript which in turn failed to handle the new location.
If neither is the case or you still cannot figure it, then we'd be interested in the smallest possible copy'n'pasteable code snippet which reproduces exactly this problem. Update then your question to include it.
Update as per the comments, the culprit is indeed in JavaScript. A redirect on a XMLHttpRequest POST isn't going to work. Are you using homegrown XMLHttpRequest functions or a library around it like as jQuery? If jQuery, please read this question carefully. It boils down to that you need to return a specific response and then let JS/jQuery do the new window.location itself.
Turns out that it was the JavaScript I was using to send the POST that was the problem.
I originally had this:
Delete
And everything got fixed when I changed it to this:
Delete
The deletePhoto function is:
function deletePhoto(photoID) {
doPost(document.URL, {'action':'delete', 'id':photoID});
}
function doPost(path, params) {
var form = document.createElement("form");
form.setAttribute("method", "POST");
form.setAttribute("action", path);
for(var key in params) {
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", key);
hiddenField.setAttribute("value", params[key]);
form.appendChild(hiddenField);
}
document.body.appendChild(form);
form.submit();
}

Categories

Resources