enisz / igdb

IGDB PHP API Wrapper
GNU General Public License v3.0
27 stars 8 forks source link

Api V4 #10

Closed Razorjack74 closed 4 years ago

Razorjack74 commented 4 years ago

Hello, do you have plans to upgrade your class to v4?

enisz commented 4 years ago

Hi @Razorjack74 ,

Unfortunately I dont have as much free time as I need to update the class for the v4 API. However, a few weeks ago I did some changes and updates which are not tested yet. You can find the updated class on the branch 9-new-api-version.

Feel free to test it.

The changes will be pushed to master when the readme file is updated properly.

enisz commented 4 years ago

Hi @Razorjack74,

Today I did the readme update and did a few bugfixes too. The new version is already merged to the master branch.

Feel free to open a new issue if you face any problems with the wrapper.

Cheers!

Razorjack74 commented 4 years ago

Good evening and thank you very much I will test

Razorjack74 commented 4 years ago

I bet my twitch account as written in the doc, create the token but I get this error:

Error 403: User is not authorized to access this resource with an explicit deny

enisz commented 4 years ago

Did you authenticate as well?

I'm pretty sure that you want to use your secret key instead of the access token. You have to send a post request to this url: https://id.twitch.tv/oauth2/token?client_id={your client_id}&client_secret={your client_secret} with your tokens to authorize your access token for 2 month.

There is an example on the page.

Razorjack74 commented 4 years ago

Yesterday when I tried the url https://id.twitch.tv/oauth2/token?client_id={your client_id}&client_secret={your client_secret} -> Error 404 but I will start over tonight thanks for your answer.

Pawikoski commented 4 years ago

@Razorjack74 did you repair it? I have exatcly same problem: error 403 when I'm trying to use API and 404 when I'm trying the url

Razorjack74 commented 4 years ago

@Razorjack74 did you repair it? I have exatcly same problem: error 403 when I'm trying to use API and 404 when I'm trying the url

No same problem

Razorjack74 commented 4 years ago

I just tried again and it doesn't work :-( 404 page not found

Razorjack74 commented 4 years ago

I use postman and work! for the token.

enisz commented 4 years ago

Hi Guys,

Can you confirm if you have the same result using your tokens without the wrapper?

I just tested it, and with my tokens this script

    var_dump(
        (new IGDB($client_id, $token))->game(
            array(
                "search" => "uncharted",
                "fields" => "id,name",
                "limit" => 5
            )
        )
    );

returns the below result:

array (size=5)
  0 => 
    object(stdClass)[3]
      public 'id' => int 126126
      public 'name' => string 'Uncharted Ocean' (length=15)
  1 => 
    object(stdClass)[4]
      public 'id' => int 82008
      public 'name' => string 'Uncharted Waters' (length=16)
  2 => 
    object(stdClass)[5]
      public 'id' => int 4459
      public 'name' => string 'Uncharted Waters' (length=16)
  3 => 
    object(stdClass)[6]
      public 'id' => int 112090
      public 'name' => string 'Uncharted Water IV' (length=18)
  4 => 
    object(stdClass)[7]
      public 'id' => int 14441
      public 'name' => string 'Uncharted Waters 2' (length=18)

You could try Postman for example, it should return the same result. Make sure to provide your client id and authorization token in the request headers:

image

And make sure to send your query as an apicalpyse string in the body using the "raw" option:

image

search "uncharted"; fields id,name; limit 5;

My suspicion is, that your tokens are invalid for some reason. If I'm right, you will recieve an error even with Postman.

Let me know of your results!

enisz commented 4 years ago

I just regenerated my access token, tried the same script above and it worked without any issues. Both using Postman and the wrapper.

Razorjack74 commented 4 years ago

Thanks for the doc, but after my test with Postman, I got my Token and I was able to correct my requests. Like Popularity which is removed in V4. So everything is in order for the part. Many thanks for your help.

Pawikoski commented 4 years ago

@enisz I still can't get it. I did everything as in the screenshots. The result is: image

enisz commented 4 years ago

@Pawikoski, this error message indicates that your access token is still invalid. Make sure to follow the steps as described in the Update Authentication section of the Breaking Changes in the API Documentation.


5. Update Authentication Authentication will be migrating away from the 3scale managed layer and instead be performed through the Twitch Developers program, using oauth.

Previously authentication was done by providing your API token in the user-key request header. On v4, the user-key header will be ignored and requests must use the following instead.

Client-ID: Twitch Developer Application Client ID
Authorization: Twitch Developer Application Access Token

Full information regarding acquiring a Client ID and Access Token can be found at https://dev.twitch.tv/docs/authentication.

However, to get started immediately:

  1. Sign Up with Twitch for a free account
  2. Ensure you have Two Factor Authentication enabled
  3. Register your application
  4. Manage your newly created application
  5. Generate a Client Secret by pressing [New Secret]
  6. Take note of the Client ID and Client Secret

Your application will then need to make a request following the OAuth client credentials flow documentation, replacing client_id and client_secret query string parameters with the values you generated earlier.

POST https://id.twitch.tv/oauth2/token?
    ?client_id=retgzhvpsxjwun0rvrb1rfwheegu1yw
    &client_secret=dfgh4h5iug3iug35g97131dgh3947u4
    &grant_type=client_credentials

The response will be a json object containing the access token and the number of seconds until the token expires.

{
   "access_token": "prau3ol6mg5glgek8m89ec2s9q5i3i",
   "expires_in": 5587808,
   "token_type": "bearer"
}

Requests can now be made to v4 specifying Client ID and Access Token within the headers Client-ID and Authorization.

POST https://api.igdb.com/v4/games
Client-ID: retgzhvpsxjwun0rvrb1rfwheegu1yw
Authorization: Bearer prau3ol6mg5glgek8m89ec2s9q5i3i

The expires_in time (seconds) should be used to calculate when you will need to authenticate again by creating a new access_token (usually > 60 days).

Pawikoski commented 4 years ago

It's working now. I don't know what was wrong. I generated access token ~15 times and the 15th is working. Anyway, thank you for help

gitadam0 commented 1 year ago

https://id.twitch.tv/oauth2/token? ?client_id=retgzhvpsxjwun0rvrb1rfwheegu1yw &client_secret=dfgh4h5iug3iug35g97131dgh3947u4 &grant_type=client_credentials

hello brother i awlays get 404 page not found even tho i follow all the guide lines

enisz commented 1 year ago

@gitadam0, never expose your tokens! You may want to regenerate them as soon as possible.

After that you may want to head over to the IGDB documentation to see how authentication works. Most probably you are trying to send your request as a GET request instead of POST.

Also you can try to use IGDBUtils::authenticate to generate your token.

If you still face issues with your token please contact IGDB support for help.

gitadam0 commented 1 year ago

yes that was the error im quite new thank you so much and one more question is there a better way to get an image of a game else then cover.url and get more popular games

enisz commented 1 year ago

For images you can use the cover.url field.

<?php

    require_once("./src/class.igdb.php");
    require_once("./src/IGDBUtils.php");

    $igdb = new IGDB($client_id, $access_token);
    $builder = new IGDBQueryBuilder();
    $query = $builder
        ->id(7331)
        ->fields('name,cover.url')
        ->build();

    var_dump($igdb->game($query))

?>

The query above will result:

array (size=1)
  0 => 
    object(stdClass)[3]
      public 'id' => int 7331
      public 'cover' => 
        object(stdClass)[4]
          public 'id' => int 81917
          public 'url' => string '//images.igdb.com/igdb/image/upload/t_thumb/co1r7h.jpg' (length=54)
      public 'name' => string 'Uncharted 4: A Thief's End' (length=26)

Also you can use the IGDBUtils::image_url() helper method to get the url with the chosen resolution easier.

For popular games you may want to use the fields aggregated_rating, rating, or total_rating. You could try to order your results by these fields. However, these fields as I see are not mandatory so the results may not be too accurate.

<?php

    require_once("./src/class.igdb.php");
    require_once("./src/IGDBUtils.php");

    $igdb = new IGDB($client_id, $access_token);
    $builder = new IGDBQueryBuilder();

    $query = $builder
        ->fields('name,rating')
        ->where('rating != null') // filtering out games which doesn't have rating field
        ->sort('rating desc') // sorting by this field in descending order
        ->limit(10) // show only 10 games
        ->build();

    var_dump($igdb->game($query))

?>

The results:

array (size=10)
  0 => 
    object(stdClass)[3]
      public 'id' => int 11085
      public 'name' => string 'Age of Wonders III: Eternal Lords' (length=33)
      public 'rating' => float 100
  1 => 
    object(stdClass)[4]
      public 'id' => int 8863
      public 'name' => string 'Age of Wonders III: Golden Realms' (length=33)
      public 'rating' => float 100
  2 => 
    object(stdClass)[5]
      public 'id' => int 204350
      public 'name' => string 'The Last of Us Part I' (length=21)
      public 'rating' => float 99.865114314138
  3 => 
    object(stdClass)[6]
      public 'id' => int 21324
      public 'name' => string 'Star Realms' (length=11)
      public 'rating' => float 99.841507713647
  4 => 
    object(stdClass)[7]
      public 'id' => int 50261
      public 'name' => string 'Angband' (length=7)
      public 'rating' => float 99.828010323617
  5 => 
    object(stdClass)[8]
      public 'id' => int 204360
      public 'name' => string 'Goat Simulator 3' (length=16)
      public 'rating' => float 99.692341155715
  6 => 
    object(stdClass)[9]
      public 'id' => int 21073
      public 'name' => string 'Metal Gear Solid: Snake Eater 3D' (length=32)
      public 'rating' => float 99.684326295381
  7 => 
    object(stdClass)[10]
      public 'id' => int 20196
      public 'name' => string 'Metal Gear Solid: The Legacy Collection' (length=39)
      public 'rating' => float 99.680396062123
  8 => 
    object(stdClass)[11]
      public 'id' => int 41888
      public 'name' => string 'Battlefield 3: Premium Edition' (length=30)
      public 'rating' => float 99.636011928478
  9 => 
    object(stdClass)[12]
      public 'id' => int 51267
      public 'name' => string 'Umineko: When They Cry Chiru' (length=28)
      public 'rating' => float 99.557618224261
gitadam0 commented 1 year ago

thanks so much