1

I just want to have same query result like stackoverflow.com did. I read this post:Searching on Stack Overflow and using the API give different results?.

looks like /search/excerpts is the right choice: https://api.stackexchange.com/docs/excerpt-search#order=desc&sort=activity&q=raml%20rest&filter=!m6Wy7AFuaogF.dVbTekNC-J-hhkc1uWKI08gO_52dnbUs-bpQ7Fj).qM&site=stackoverflow&run=true

While the question is: there are many parameters, could you give an example payload which will return same query results like stackoverflow.com site did?

I'm using java, is there official java stackoverflow client which I can use?

2

There is no "official" Java client. I assume Java comes with an HTTP client in its standard library. That one should do the trick.

The minimal example you need is this URL (click it!):

https://api.stackexchange.com/2.3/search/excerpts?q=raml rest&site=stackoverflow

An HTTP Get will work, no need to Post.

So that URL has the following parameters in its query string:

Name Value
q what you want to search for
site the site to perform the API call on

The returned JSON payload is an JSON Object with an items array that holds the search_excerpt objects.

I do advice to use an extra parameter called filter with this value !nKzQUR693x. It will return in the wrapper object the number of total hits. You get at most 100 hits (set the pagesize, default is 15!) in one go so you have to make that call a couple of times if there are more then 100 results. Supplying the page parameter will allow you to fetch subsequent pages of the total result.

Combining all that gives this URL

https://api.stackexchange.com/2.3/search/excerpts?q=raml%20rest&site=stackoverflow&filter=!nKzQUR693x&page=5

For the record: The API search reports that it found a total of 240 excerpts, which is the same number found on Stack Overflow today.

For accessing the same endpoint in an Stack Overflow for Teams team you need to follow the steps mentioned in Stack Overflow for Teams API and first register an App: I only need a key, what do I do? it is important to enter in the OAuth domain the url stackexchange.com/oauth/login_success.

Once you have registered you app, you can authenticate by completing this URL and paste it in a browser:

https://stackoverflow.com/oauth/dialog?client_id=<client_id>&scope=no_expiry access_team|stackoverflow.com/c/<team name>&redirect_uri=https://stackexchange.com/oauth/login_success

Notice the <client_id> and <team name> bits that needs to be replaced (including the < and > ) by values you only can know. Client_Id is found on your App registration page.

When the URL did its thing and you granted access the response in the url contains your access_token.

Now you have all the parameters you need to make the call for a Team:

Name Value
q what you want to search for
site the site to perform the API call on
key the key as found on your App registration page
team stackoverflow.com/c/<your team name>

In the HTTP Headers you send this:

Name Value
X-API-Access-Token The access_token you got when you authenticated your App

That completes the extra bits you need for calling that search/excerpts endpoint for a Team instead of a public SE site.

To be complete, here is a JavaScript Example:

var url = 'https://api.stackexchange.com/2.3/search/excerpts?q=raml%20rest&site=stackoverflow&filter=!nKzQUR693x';

var inTeams = true;

var fetchinit = {} // for headers

if (inTeams) {
  var yourTeam = '<team name here>' 
  var yourAccessToken = '<your access token>';
  var yourKey = '<your key here>';

  url = url + '&key=' + yourKey
  url = url + '&team=stackoverflow.com/c/' + yourTeam;
  
  fetchinit.headers = {'X-API-Access-Token' : yourAccessToken }
}


function build(i) {
  console.log(i.title);
}

fetch(url, fetchinit)
  .then((resp)=> resp.json())
  .then((json) => json.items)
  .then((items) => items.forEach(build));

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .