locked
OData and ApiKey url-parameter through ServiceReference RRS feed

  • Question

  • ApiKey is used to control the usage and it is affixed as an url parameter (implemented using somewhat modified version of this solution). Question is, how do you go about appending this parameter when consuming the API through the use of the ServiceReference in Visual Studio?
    Friday, April 22, 2011 2:58 AM

Answers

  • You can do this by using the AddQueryOption method on DataServiceQuery<TElement> like this:

    var people = from person in service.Person.AddQueryOption("apiKey", 1234)
    
       select person;
    
    
    
    foreach(var person in people)
    
    {
    
      // ... do stuff...
    
    }
    
    

    The generated URI should then include the ?apiKey=1234 query parameter.

    Cheers,

    Glenn Gailey


    This posting is provided "AS IS" with no warranties, and confers no rights.
    • Marked as answer by hukim0 Monday, April 25, 2011 12:47 AM
    Saturday, April 23, 2011 6:28 AM

All replies

  • Hi,

    Although VS has no support for customizing the request sent during the "Add Service Reference" dialog right now, if your apiKey is a query option, it seems to work. For example I asked VS to Add Service Reference to this URL: http://services.odata.org/OData/OData.svc/$metadata?apiKey=1234 and it worked just fine (confirmed with Fiddler that it did actually send the query string as is).

    Now the demo service on odata.org will ignore the additional query string, but your own service may not.

    The other option is to use the command line datasvcutil.exe where you get to specify the exact URL.

    Thanks,


    Vitek Karas [MSFT]
    Friday, April 22, 2011 3:03 PM
    Moderator
  • Hi,

    First of all, thank you for a quick answer. However, I must have not been clear with my question. During "Add Service Reference", yes you can add the extra parameter. But the question is while consuming it. Are you suggesting that when you query against the Service Reference, 'apikey=1234' would automatically append to subsequent calls/queries to the Service Reference?

    For example, if I wanted to query for all the people as below using the added service reference,

    var people = from person in service.Person
              select person;
    
    foreach(var person in people)
    {
     ... do stuff...
    }

    Will this generate URL, 

    http://host/OData.svc/Person?apikey=1234

    if not, how do I go about adding this to every call to the service. After all, that is the purpose of an ApiKey.

    Thank you in advance.

    Friday, April 22, 2011 5:44 PM
  • You can do this by using the AddQueryOption method on DataServiceQuery<TElement> like this:

    var people = from person in service.Person.AddQueryOption("apiKey", 1234)
    
       select person;
    
    
    
    foreach(var person in people)
    
    {
    
      // ... do stuff...
    
    }
    
    

    The generated URI should then include the ?apiKey=1234 query parameter.

    Cheers,

    Glenn Gailey


    This posting is provided "AS IS" with no warranties, and confers no rights.
    • Marked as answer by hukim0 Monday, April 25, 2011 12:47 AM
    Saturday, April 23, 2011 6:28 AM
  • Perfect! Thank you Glenn. 
    Monday, April 25, 2011 12:46 AM