If I understand your question correctly, you want to use webentityusage
in your query to get the wikidata ids. So, for your particular example, with London, you would use:
https://en.wikipedia.org/w/api.php?action=query&prop=pageprops|wbentityusage&titles=London
The entry at the bottom has all the data associated with London:
Scroll down in the JSON and you'll see:
"wbentityusage": {
...
"Q84": {
...
}
}
Q84 also shows as the "wikibase_item" under pageprops.
Then, all the data associated with london is at:
https://www.wikidata.org/wiki/Q84
(which can also be accessed via wikidata apis)
EDIT: Here's yet another example. (for wikidata-specific items, it can sometimes help to work in reverse...i.e., list all the pages that reference data Q[nnn])
The following wikipedia page uses some wikidata item(s): https://en.wikipedia.org/wiki/Template:Pageid_to_title
Specifically, it uses Earth (Q2). So, if we use the wikipedia API:
https://en.wikipedia.org/w/api.php?action=query&prop=pageprops|wbentityusage&titles=Template:Pageid_to_title
It will show Q2 under the wbentityusage
for the pageId 49086285. Not under pageprops. Pageprops will only show the wikibase_item.
It's important to note that even though the query is identical on both wikipedia and wikidata, the results will be different based on which domain you run it on.
Also helpful (work in reverse): to see what wikipedia pages reference a particular wikidata item, you would use
https://en.wikipedia.org/w/api.php?action=query&list=wblistentityusage&wbeuentities=Q2&wbeuprop=url
--> Shows all the wikipedia pages referencing Q2 (Earth)
https://wikidata.org/w/api.php?action=query&list=wblistentityusage&wbeuentities=Q2&wbeuprop=url
--> Shows all the wikidata pages referencing Q2 (Earth)
And, just for fun, I edited that wikipedia page Template:Pageid_to_title to reference Q3 (Life) and...after a little bit of patience waiting for things to sync, the API now responds with Q3 as also being one of the wikidata items associated with that page.