Python Google Photos API List Items in Album










0















I am having trouble extending the API calls required to list album ids to be able to list the items contained in the album. An example of what I have done to retrieve album ids appears below. Beyond that example, I include my attempt to extend this script to be able to list the items contained in a specific album.



from httplib2 import Http
from oauth2client import file, client, tools
from apiclient.discovery import build

SCOPES = 'https://www.googleapis.com/auth/photoslibrary.readonly'
store = file.storage('credentials.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
creds = tools.run_flow(flow, store)
service = build('photoslibrary', 'v1', http=creds.authorize(Http()))

hasNextPageToken = False
nextPageToken = ""

results = service.albums().list(pageSize=10).execute()

if 'nextPageToken' in results:
hasNextPageToken = True
nextPageToken = results['nextPageToken']
for album in results['albums']:
albumId = album['id']
print(albumId)


Now, for the code that I am attempting to modify from the example above to be able to list the items in a given album defined by albumId:



from httplib2 import Http
from oauth2client import file, client, tools
from apiclient.discovery import build

SCOPES = 'https://www.googleapis.com/auth/photoslibrary.readonly'
store = file.storage('credentials.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
creds = tools.run_flow(flow, store)
service = build('photoslibrary', 'v1', http=creds.authorize(Http()))

hasNextPageToken = False
nextPageToken = ""
albumId = [albumIdFromPreviousStep]
results = service.mediaItems().search(pageSize=10,albumId=albumId).execute()


When I attempt to print the results, I receive an error about receiving an unexpected keyword argument, albumId.



The api call is formatted differently for search, it is https://photoslibrary.googleapis.com/v1/mediaItems:search



Is the error stemming from the : mediaItems:search which invalidates the variable in Python? Or is it related to the fact that the search call is a POST rather than a GET? Or, something else?










share|improve this question




























    0















    I am having trouble extending the API calls required to list album ids to be able to list the items contained in the album. An example of what I have done to retrieve album ids appears below. Beyond that example, I include my attempt to extend this script to be able to list the items contained in a specific album.



    from httplib2 import Http
    from oauth2client import file, client, tools
    from apiclient.discovery import build

    SCOPES = 'https://www.googleapis.com/auth/photoslibrary.readonly'
    store = file.storage('credentials.json')
    creds = store.get()
    if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
    creds = tools.run_flow(flow, store)
    service = build('photoslibrary', 'v1', http=creds.authorize(Http()))

    hasNextPageToken = False
    nextPageToken = ""

    results = service.albums().list(pageSize=10).execute()

    if 'nextPageToken' in results:
    hasNextPageToken = True
    nextPageToken = results['nextPageToken']
    for album in results['albums']:
    albumId = album['id']
    print(albumId)


    Now, for the code that I am attempting to modify from the example above to be able to list the items in a given album defined by albumId:



    from httplib2 import Http
    from oauth2client import file, client, tools
    from apiclient.discovery import build

    SCOPES = 'https://www.googleapis.com/auth/photoslibrary.readonly'
    store = file.storage('credentials.json')
    creds = store.get()
    if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
    creds = tools.run_flow(flow, store)
    service = build('photoslibrary', 'v1', http=creds.authorize(Http()))

    hasNextPageToken = False
    nextPageToken = ""
    albumId = [albumIdFromPreviousStep]
    results = service.mediaItems().search(pageSize=10,albumId=albumId).execute()


    When I attempt to print the results, I receive an error about receiving an unexpected keyword argument, albumId.



    The api call is formatted differently for search, it is https://photoslibrary.googleapis.com/v1/mediaItems:search



    Is the error stemming from the : mediaItems:search which invalidates the variable in Python? Or is it related to the fact that the search call is a POST rather than a GET? Or, something else?










    share|improve this question


























      0












      0








      0


      2






      I am having trouble extending the API calls required to list album ids to be able to list the items contained in the album. An example of what I have done to retrieve album ids appears below. Beyond that example, I include my attempt to extend this script to be able to list the items contained in a specific album.



      from httplib2 import Http
      from oauth2client import file, client, tools
      from apiclient.discovery import build

      SCOPES = 'https://www.googleapis.com/auth/photoslibrary.readonly'
      store = file.storage('credentials.json')
      creds = store.get()
      if not creds or creds.invalid:
      flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
      creds = tools.run_flow(flow, store)
      service = build('photoslibrary', 'v1', http=creds.authorize(Http()))

      hasNextPageToken = False
      nextPageToken = ""

      results = service.albums().list(pageSize=10).execute()

      if 'nextPageToken' in results:
      hasNextPageToken = True
      nextPageToken = results['nextPageToken']
      for album in results['albums']:
      albumId = album['id']
      print(albumId)


      Now, for the code that I am attempting to modify from the example above to be able to list the items in a given album defined by albumId:



      from httplib2 import Http
      from oauth2client import file, client, tools
      from apiclient.discovery import build

      SCOPES = 'https://www.googleapis.com/auth/photoslibrary.readonly'
      store = file.storage('credentials.json')
      creds = store.get()
      if not creds or creds.invalid:
      flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
      creds = tools.run_flow(flow, store)
      service = build('photoslibrary', 'v1', http=creds.authorize(Http()))

      hasNextPageToken = False
      nextPageToken = ""
      albumId = [albumIdFromPreviousStep]
      results = service.mediaItems().search(pageSize=10,albumId=albumId).execute()


      When I attempt to print the results, I receive an error about receiving an unexpected keyword argument, albumId.



      The api call is formatted differently for search, it is https://photoslibrary.googleapis.com/v1/mediaItems:search



      Is the error stemming from the : mediaItems:search which invalidates the variable in Python? Or is it related to the fact that the search call is a POST rather than a GET? Or, something else?










      share|improve this question
















      I am having trouble extending the API calls required to list album ids to be able to list the items contained in the album. An example of what I have done to retrieve album ids appears below. Beyond that example, I include my attempt to extend this script to be able to list the items contained in a specific album.



      from httplib2 import Http
      from oauth2client import file, client, tools
      from apiclient.discovery import build

      SCOPES = 'https://www.googleapis.com/auth/photoslibrary.readonly'
      store = file.storage('credentials.json')
      creds = store.get()
      if not creds or creds.invalid:
      flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
      creds = tools.run_flow(flow, store)
      service = build('photoslibrary', 'v1', http=creds.authorize(Http()))

      hasNextPageToken = False
      nextPageToken = ""

      results = service.albums().list(pageSize=10).execute()

      if 'nextPageToken' in results:
      hasNextPageToken = True
      nextPageToken = results['nextPageToken']
      for album in results['albums']:
      albumId = album['id']
      print(albumId)


      Now, for the code that I am attempting to modify from the example above to be able to list the items in a given album defined by albumId:



      from httplib2 import Http
      from oauth2client import file, client, tools
      from apiclient.discovery import build

      SCOPES = 'https://www.googleapis.com/auth/photoslibrary.readonly'
      store = file.storage('credentials.json')
      creds = store.get()
      if not creds or creds.invalid:
      flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
      creds = tools.run_flow(flow, store)
      service = build('photoslibrary', 'v1', http=creds.authorize(Http()))

      hasNextPageToken = False
      nextPageToken = ""
      albumId = [albumIdFromPreviousStep]
      results = service.mediaItems().search(pageSize=10,albumId=albumId).execute()


      When I attempt to print the results, I receive an error about receiving an unexpected keyword argument, albumId.



      The api call is formatted differently for search, it is https://photoslibrary.googleapis.com/v1/mediaItems:search



      Is the error stemming from the : mediaItems:search which invalidates the variable in Python? Or is it related to the fact that the search call is a POST rather than a GET? Or, something else?







      python google-api google-photos-api






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 21:38







      Daniel

















      asked Nov 15 '18 at 21:24









      DanielDaniel

      199212




      199212






















          2 Answers
          2






          active

          oldest

          votes


















          1














          The mediaitems:search requires a JSON-structured body of parameters in order to restrict the returned items to a specific albumId.



          Modifying the second part of the code from the original post gives the desired result.



          from httplib2 import Http
          from oauth2client import file, client, tools
          from apiclient.discovery import build

          SCOPES = 'https://www.googleapis.com/auth/photoslibrary.readonly'
          store = file.storage('credentials.json')
          creds = store.get()
          if not creds or creds.invalid:
          flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
          creds = tools.run_flow(flow, store)
          service = build('photoslibrary', 'v1', http=creds.authorize(Http()))

          hasNextPageToken = False
          nextPageToken = ""
          body =
          "albumId": [AlbumId],
          "pageSize": 10

          results = service.mediaItems().search(body=body).execute()
          print(results)


          Now, collecting the data for all the other items requires incorporating the nextPageToken and looping through the results.






          share|improve this answer






























            0














            i cant see anywhere you defined albumId in your modified code



            results = service.mediaItems().search(pageSize=10,**albumId=albumId**).execute()


            the code you copied from clearly defines it



            for album in results['albums']:
            albumId = album['id']
            print(albumId)





            share|improve this answer























            • you're right - I missed that, the original will be updated soon. Thanks for the catch

              – Daniel
              Nov 15 '18 at 21:36











            • you'll probably make two calls, one to grab the id and the second to loop through

              – sunny chidi
              Nov 15 '18 at 21:41











            • I agree - I am seeking help on the 2nd call. It employs a different service from the one used to list album ids and I believe I am not setting that service correctly.

              – Daniel
              Nov 15 '18 at 21:46










            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53328101%2fpython-google-photos-api-list-items-in-album%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            The mediaitems:search requires a JSON-structured body of parameters in order to restrict the returned items to a specific albumId.



            Modifying the second part of the code from the original post gives the desired result.



            from httplib2 import Http
            from oauth2client import file, client, tools
            from apiclient.discovery import build

            SCOPES = 'https://www.googleapis.com/auth/photoslibrary.readonly'
            store = file.storage('credentials.json')
            creds = store.get()
            if not creds or creds.invalid:
            flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
            creds = tools.run_flow(flow, store)
            service = build('photoslibrary', 'v1', http=creds.authorize(Http()))

            hasNextPageToken = False
            nextPageToken = ""
            body =
            "albumId": [AlbumId],
            "pageSize": 10

            results = service.mediaItems().search(body=body).execute()
            print(results)


            Now, collecting the data for all the other items requires incorporating the nextPageToken and looping through the results.






            share|improve this answer



























              1














              The mediaitems:search requires a JSON-structured body of parameters in order to restrict the returned items to a specific albumId.



              Modifying the second part of the code from the original post gives the desired result.



              from httplib2 import Http
              from oauth2client import file, client, tools
              from apiclient.discovery import build

              SCOPES = 'https://www.googleapis.com/auth/photoslibrary.readonly'
              store = file.storage('credentials.json')
              creds = store.get()
              if not creds or creds.invalid:
              flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
              creds = tools.run_flow(flow, store)
              service = build('photoslibrary', 'v1', http=creds.authorize(Http()))

              hasNextPageToken = False
              nextPageToken = ""
              body =
              "albumId": [AlbumId],
              "pageSize": 10

              results = service.mediaItems().search(body=body).execute()
              print(results)


              Now, collecting the data for all the other items requires incorporating the nextPageToken and looping through the results.






              share|improve this answer

























                1












                1








                1







                The mediaitems:search requires a JSON-structured body of parameters in order to restrict the returned items to a specific albumId.



                Modifying the second part of the code from the original post gives the desired result.



                from httplib2 import Http
                from oauth2client import file, client, tools
                from apiclient.discovery import build

                SCOPES = 'https://www.googleapis.com/auth/photoslibrary.readonly'
                store = file.storage('credentials.json')
                creds = store.get()
                if not creds or creds.invalid:
                flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
                creds = tools.run_flow(flow, store)
                service = build('photoslibrary', 'v1', http=creds.authorize(Http()))

                hasNextPageToken = False
                nextPageToken = ""
                body =
                "albumId": [AlbumId],
                "pageSize": 10

                results = service.mediaItems().search(body=body).execute()
                print(results)


                Now, collecting the data for all the other items requires incorporating the nextPageToken and looping through the results.






                share|improve this answer













                The mediaitems:search requires a JSON-structured body of parameters in order to restrict the returned items to a specific albumId.



                Modifying the second part of the code from the original post gives the desired result.



                from httplib2 import Http
                from oauth2client import file, client, tools
                from apiclient.discovery import build

                SCOPES = 'https://www.googleapis.com/auth/photoslibrary.readonly'
                store = file.storage('credentials.json')
                creds = store.get()
                if not creds or creds.invalid:
                flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
                creds = tools.run_flow(flow, store)
                service = build('photoslibrary', 'v1', http=creds.authorize(Http()))

                hasNextPageToken = False
                nextPageToken = ""
                body =
                "albumId": [AlbumId],
                "pageSize": 10

                results = service.mediaItems().search(body=body).execute()
                print(results)


                Now, collecting the data for all the other items requires incorporating the nextPageToken and looping through the results.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 16 '18 at 1:27









                DanielDaniel

                199212




                199212























                    0














                    i cant see anywhere you defined albumId in your modified code



                    results = service.mediaItems().search(pageSize=10,**albumId=albumId**).execute()


                    the code you copied from clearly defines it



                    for album in results['albums']:
                    albumId = album['id']
                    print(albumId)





                    share|improve this answer























                    • you're right - I missed that, the original will be updated soon. Thanks for the catch

                      – Daniel
                      Nov 15 '18 at 21:36











                    • you'll probably make two calls, one to grab the id and the second to loop through

                      – sunny chidi
                      Nov 15 '18 at 21:41











                    • I agree - I am seeking help on the 2nd call. It employs a different service from the one used to list album ids and I believe I am not setting that service correctly.

                      – Daniel
                      Nov 15 '18 at 21:46















                    0














                    i cant see anywhere you defined albumId in your modified code



                    results = service.mediaItems().search(pageSize=10,**albumId=albumId**).execute()


                    the code you copied from clearly defines it



                    for album in results['albums']:
                    albumId = album['id']
                    print(albumId)





                    share|improve this answer























                    • you're right - I missed that, the original will be updated soon. Thanks for the catch

                      – Daniel
                      Nov 15 '18 at 21:36











                    • you'll probably make two calls, one to grab the id and the second to loop through

                      – sunny chidi
                      Nov 15 '18 at 21:41











                    • I agree - I am seeking help on the 2nd call. It employs a different service from the one used to list album ids and I believe I am not setting that service correctly.

                      – Daniel
                      Nov 15 '18 at 21:46













                    0












                    0








                    0







                    i cant see anywhere you defined albumId in your modified code



                    results = service.mediaItems().search(pageSize=10,**albumId=albumId**).execute()


                    the code you copied from clearly defines it



                    for album in results['albums']:
                    albumId = album['id']
                    print(albumId)





                    share|improve this answer













                    i cant see anywhere you defined albumId in your modified code



                    results = service.mediaItems().search(pageSize=10,**albumId=albumId**).execute()


                    the code you copied from clearly defines it



                    for album in results['albums']:
                    albumId = album['id']
                    print(albumId)






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 15 '18 at 21:34









                    sunny chidisunny chidi

                    8425




                    8425












                    • you're right - I missed that, the original will be updated soon. Thanks for the catch

                      – Daniel
                      Nov 15 '18 at 21:36











                    • you'll probably make two calls, one to grab the id and the second to loop through

                      – sunny chidi
                      Nov 15 '18 at 21:41











                    • I agree - I am seeking help on the 2nd call. It employs a different service from the one used to list album ids and I believe I am not setting that service correctly.

                      – Daniel
                      Nov 15 '18 at 21:46

















                    • you're right - I missed that, the original will be updated soon. Thanks for the catch

                      – Daniel
                      Nov 15 '18 at 21:36











                    • you'll probably make two calls, one to grab the id and the second to loop through

                      – sunny chidi
                      Nov 15 '18 at 21:41











                    • I agree - I am seeking help on the 2nd call. It employs a different service from the one used to list album ids and I believe I am not setting that service correctly.

                      – Daniel
                      Nov 15 '18 at 21:46
















                    you're right - I missed that, the original will be updated soon. Thanks for the catch

                    – Daniel
                    Nov 15 '18 at 21:36





                    you're right - I missed that, the original will be updated soon. Thanks for the catch

                    – Daniel
                    Nov 15 '18 at 21:36













                    you'll probably make two calls, one to grab the id and the second to loop through

                    – sunny chidi
                    Nov 15 '18 at 21:41





                    you'll probably make two calls, one to grab the id and the second to loop through

                    – sunny chidi
                    Nov 15 '18 at 21:41













                    I agree - I am seeking help on the 2nd call. It employs a different service from the one used to list album ids and I believe I am not setting that service correctly.

                    – Daniel
                    Nov 15 '18 at 21:46





                    I agree - I am seeking help on the 2nd call. It employs a different service from the one used to list album ids and I believe I am not setting that service correctly.

                    – Daniel
                    Nov 15 '18 at 21:46

















                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53328101%2fpython-google-photos-api-list-items-in-album%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    27

                    Top Tejano songwriter Luis Silva dead of heart attack at 64

                    Category:Rhetoric