producthunt / producthunt-api

Product Hunt API
311 stars 28 forks source link

XMLHttpRequest Errors when using developer token #66

Closed euccas closed 9 years ago

euccas commented 9 years ago

I'm building an app which need read access from product hunt API. I registered my app on the api dashboard as the following:

redirect URL: http://euccas.name/eph (This link does not exist though)

I also created a developer_token for testing purpose.

The problem I'm facing is the request to product hunt api always fails either when asking for a client level token, or asking for the posts by using my developer_token. The errors are:

OPTIONS https://api.producthunt.com/v1/posts cachePH @ eph.html:27onclick @ eph.html:37
eph.html:1 XMLHttpRequest cannot load https://api.producthunt.com/v1/posts. No 'Access-Control-  Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.    The response had HTTP status code 404.

My code:

     // ask for client level oAuth token
     // https://api.producthunt.com/v1/docs/oauth_client_only_authentication/oauth_token_ask_for_client_level_token
     var auth = new XMLHttpRequest();
     auth.open("POST", "https://api.producthunt.com/v1/oauth/token", true);
     auth.setRequestHeader("Accept", "application/json");
     auth.setRequestHeader("Content-type","application/json");
     auth.send({"client_id":   "9be433a2ab3305fa88987b1cdae255241009229c0fe14e7be72248b354be1b73", "client_secret": "d76d028b28eb800907b5c0a7ba0a101f658a6bb4fea376dfcbab8901757447b4", "grant_type": "client_credentials"});

     console.log(auth.status);
     console.log(auth.statusText);  

    // read api access
    var access = new XMLHttpRequest();
    access.open("GET", "https://api.producthunt.com/v1/posts");
    access.setRequestHeader("Accept", "application/json");
    access.setRequestHeader("Content-type", "application/json");
    access.setRequestHeader("Authorization", "Bearer 3511d49fde8d3ea111df442b1a265fa78cf1e09d54076c17ca5078cbab15e772 ");

    access.send();
    console.log(access.status);
    console.log(access.statusText);
euccas commented 9 years ago

Does product hunt api only allow javascript to be executed from the server which I set as 'Redirect URL' when creating the app in the api dashboard?

euccas commented 9 years ago

I managed to get it work with the help of Mashape. Now looking for the cause why my previous attempts failed.