husnjak / IGDB-API-JVM

Wrapper for IGDBs API written in Kotlin should work for all JVM based languages, tested in Kotlin & Java
MIT License
40 stars 11 forks source link

RequestException #8

Closed ghost closed 4 years ago

ghost commented 4 years ago

Hi, I tried implement the Java code as shown in README, but the following exception is thrown:

com.api.igdb.exceptions.RequestException at com.api.igdb.request.IGDBWrapper.apiJsonRequest(IGDBWrapper.kt:60)

When I use the getStatusCode method it returns "403"

husnjak commented 4 years ago

Hello, Can you give me a specific code example? Also 403 is forbidden, have you added your client_id and access token?

IGDBWrapper wrapper = IGDBWrapper.INSTANCE;
wrapper.setCredentials("CLIENT_ID", "ACCESS_TOKEN")
ghost commented 4 years ago

I'm using the same codes that I put in the constants to test on Insomnia and it works.

 TwitchAuthenticator tAuth = TwitchAuthenticator.INSTANCE;
        TwitchToken token = tAuth.requestTwitchToken(CLIENT_ID, CLIENT_SECRET);
        IGDBWrapper wrapper = IGDBWrapper.INSTANCE;
        wrapper.setCredentials(CLIENT_ID, token.getAccess_token());
        APICalypse apiCalypse = new APICalypse()
                .fields("id, name, platforms")
                .limit(10)
                .search("Zelda");
        try {
            String json = JsonRequestKt.jsonGames(wrapper, apiCalypse);
            System.out.println(json);
        } catch (RequestException e) {
            System.out.println(e.getStatusCode());
            throw e;
        }
husnjak commented 4 years ago

I can get your code working by adding my own credentials. And you get this working in Insomnia, can you try pasting the access token directly in token.getAccess_token() (without Bearer keyword). Also if you set a breakpoint in the catch you can check e.request and compare those credentials to what you have and make sure that those work

ghost commented 4 years ago

The same problem continues. I checked e.request and it exactly return id and token that i added directly as parameters. Therefore, i don't know why the 403 Forbidden problem.

husnjak commented 4 years ago

There isn't much I can do without your credentials, as I cannot test your specific account.

I believe that it has to do something with your credentials in code, as I can get this exact code working with my credentials and you said that you can use your credentials in insomnia without issue.

husnjak commented 4 years ago

I've removed your comment because you just posted your credentials for all to see, I strongly suggest that you update your client_secret.

When I used your credentials everything seemed to work just fine, I managed to get a new access_token and I managed to get a response from the API

public static void main(String[] args) {
        TwitchAuthenticator auth = TwitchAuthenticator.INSTANCE;
        IGDBWrapper wrapper = IGDBWrapper.INSTANCE;

        TwitchToken token = auth.requestTwitchToken("client_id", "client_secret");

        assert token != null;
        wrapper.setCredentials("client_id", token.getAccess_token());
        APICalypse apicalypse = new APICalypse()
                .fields("id, name, platforms")
                .limit(10)
                .search("Zelda");
        try{
            String json = JsonRequestKt.jsonGames(wrapper, apicalypse);
            System.out.println(json);
        } catch(RequestException e) {
            // Do something or error
            System.out.print(e.getStatusCode());
        }

    }
ghost commented 4 years ago

Ok, i tried this in Intellij and worked, i should have tried this before 😅. But, i want to use in an Android Studio project, where this error keeps happening.

Yes, I know, you recommend creating a server to use Twitch Auth, but that’s not necessary if I’m testing on just one device, isn’t it? I wanted to see if it worked before create.

husnjak commented 4 years ago

Might be threading issues. This is my test of the wrapper for Android

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val policy = ThreadPolicy.Builder().permitAll().build()

        StrictMode.setThreadPolicy(policy)

        IGDBWrapper.setCredentials("client_id", "access_token")
        try {
            val games = IGDBWrapper.games(APICalypse())
            println(games)
        } catch (e: Exception) {
            println(e.stackTrace)
        }

    }
}

Android does not allow networking on the main thread, so you should wrap this wrapper in something that runs the calls in another thread.

ghost commented 4 years ago

It worked. Thanks bro, this is the second time that I use an API in an android project and in the first time I had no problems with thread. So I didn't even remember that the android wouldn't allow it.

Sorry to bother you with my beginner problems. 😅

Thanks a lot for the help!!!!! 😄

gitadam0 commented 1 year ago

Might be threading issues. This is my test of the wrapper for Android

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val policy = ThreadPolicy.Builder().permitAll().build()

        StrictMode.setThreadPolicy(policy)

        IGDBWrapper.setCredentials("client_id", "access_token")
        try {
            val games = IGDBWrapper.games(APICalypse())
            println(games)
        } catch (e: Exception) {
            println(e.stackTrace)
        }

    }
}

Android does not allow networking on the main thread, so you should wrap this wrapper in something that runs the calls in another thread. /////////////

hello brother Im quite new I didnt get the last part that you said about threads if you could help, I just posted my issue