Adding a URL that has an api key to the restlibrary

62 views
Skip to first unread message

Will Hanschell

unread,
May 8, 2012, 3:21:46 PM5/8/12
to Excel Ramblings
Hello all.

I'm trying to add the Klout API to the restlibrary and am having some
difficulty. I think this is because the API's URL is a different
structure to all those in the library already. Whereas with existing
URLs, the query (in this case a Twitter screenName) is simply appended
to the end of the URL when making the API call, with this URL the
query needs to be inserted into the middle of the URL as the "key"
must be placed after it. (see below)

Here's my library:

With .add("klout id")
.add "restType", erQueryPerRow
.add "url", "http://api.klout.com/v2/identity.json/twitter?
screenName=xxxxx&key=xxxxx"
.add "id"
.add "treeSearch", True
.add "ignore", vbNullString
End With

I think that this whole issue is handled later on in the library - in
the restquery function. Somewhere here:

Set cr = New cRest
' lets get the data
If (sheetName <> vbNullString) Then
Set dSet = New cDataSet
With dSet.populateData(wholeSheet(sheetName), ,
sheetName, , , , True)
' ensure that the query column exists if it was asked for
If qType = erQueryPerRow Then
If Not .headingRow.validate(True, sQueryColumn) Then
Exit Function
End If
' alsmost there
Set cr = cr.init(sResults,
qType, .headingRow.exists(sQueryColumn), _
, dSet, bPopulate, sUrl, bClearMissing, _
bTreeSearch, complain, sIgnore)
End With

Does anybody have any ideas on this? Many hours so far and still
scratching my head.

Thanks!

Will

Bruce Mcpherson

unread,
May 8, 2012, 3:30:47 PM5/8/12
to excel-r...@googlegroups.com

Will you can probably reverse the parameters on the URL with no problem

--
You received this message because you are subscribed to the Google Groups "Excel Ramblings" group.
To post to this group, send email to excel-r...@googlegroups.com.
To unsubscribe from this group, send email to excel-ramblin...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/excel-ramblings?hl=en.

Bruce Mcpherson

unread,
May 8, 2012, 7:06:16 PM5/8/12
to excel-r...@googlegroups.com
Will

I dont know if you noticed the rest explorer... you can use this to play around with what your rest query returns, as well as play around with the parameters and URL.

http://ramblings.mcpher.com/Home/excelquirks/json/restexplore

As I mentioned,
"http://api.klout.com/v2/identity.json/twitter?
screenName=xxxxx&key=xxxxx
"

..should be able to become
"http://api.klout.com/v2/identity.json/twitter?
key=xxxxx
&screenName=xxxxx"

btw .. the queryPerRow you specify, performs multiple queries - one for each row in your input data. Is that what you want ? Sounds like you want to give one single query and get multiple answers...

bruce


Will

Will Hanschell

unread,
May 9, 2012, 4:24:12 AM5/9/12
to Excel Ramblings
Brilliant Bruce, thank you - this is such a great resource.

Your advice makes total sense and works perfectly for the URL I
supplied. However, my issue still applies for the second call I need
to make.

Klout requires that you call their API once to convert a Twitter
screenName (the rows in my spreadsheet) to a Klout ID. (This is what
you helped me with before.)
You then call the API again to lookup a Klout score for that Klout ID.
The URL for that is "http://api.klout.com/v2/user.json/<QUERY>/score?
key=xxxx" where <QUERY> is the query supplied, and here it's
hierarchical I believe so I can't just swap the terms?

These are probably very simple questions but this is the final step in
this hack! So close!

Will

On May 9, 12:06 am, Bruce Mcpherson <br...@mcpher.com> wrote:
> Will
>
> I dont know if you noticed the rest explorer... you can use this to play
> around with what your rest query returns, as well as play around with the
> parameters and URL.
>
> http://ramblings.mcpher.com/Home/excelquirks/json/restexplore
>
> As I mentioned,
> "http://api.klout.com/v2/identity.json/twitter?
> screenName=xxxxx&key=xxxxx<http://api.klout.com/v2/identity.json/twitter?%0AscreenName=xxxxx&key...>
> "
>
> ..should be able to become
> "http://api.klout.com/v2/identity.json/twitter?
> key=xxxxx<http://api.klout.com/v2/identity.json/twitter?%0AscreenName=xxxxx&key...>
> &screenName=xxxxx<http://api.klout.com/v2/identity.json/twitter?%0AscreenName=xxxxx&key...>
> "
>
> btw .. the queryPerRow you specify, performs multiple queries - one for
> each row in your input data. Is that what you want ? Sounds like you want
> to give one single query and get multiple answers...
>
> bruce
>
> On 8 May 2012 14:21, Will Hanschell <will.hansch...@gmail.com> wrote:
>
>
>
>
>
>
>
> > Hello all.
>
> > I'm trying to add the Klout API to the restlibrary and am having some
> > difficulty. I think this is because the API's URL is a different
> > structure to all those in the library already. Whereas with existing
> > URLs, the query (in this case a Twitter screenName) is simply appended
> > to the end of the URL when making the API call, with this URL the
> > query needs to be inserted into the middle of the URL as the "key"
> > must be placed after it. (see below)
>
> > Here's my library:
>
> >        With .add("klout id")
> >            .add "restType", erQueryPerRow
> >            .add "url", "http://api.klout.com/v2/identity.json/twitter?
> > screenName=xxxxx&key=xxxxx<http://api.klout.com/v2/identity.json/twitter?%0AscreenName=xxxxx&key...>

Bruce Mcpherson

unread,
May 9, 2012, 11:19:18 AM5/9/12
to excel-r...@googlegroups.com
Will

ok .. a couple of solutions.
  • you make the query include the key each time..
  • you dont bother with adding your term to the library, and just passed the complete URL to the crest class

bruce

2. you query parameter would include the key
Message has been deleted

Jeff Stoddard

unread,
May 10, 2012, 8:11:51 PM5/10/12
to Excel Ramblings
Iris (wonderbra? - are you an alter ego of Will?) & Bruce -

Perhaps I can shed some light as I'm having a similar problem. I need
to construct a URL with the query in the middle as well.

my format is: xyz.com/merchant/<ID>/loyalties which if I simply type
in xyz.com/merchant/189/loyalties in a browser spits back JSON to me

I can enter all this as a URL in the restLibrary and it will work
fine, but to get it to work, I have to hard code the merchant ID by
using "xyz.com/merchant/189/loyalties" as the URL. Of course, I'd like
to be able to change the merchant ID by row.

If I try to put 189/loyalties as the parameter it does not work as the
slash is converted to "%2F" and it gives me a 404 error

If I enter xyz.com/merchant/189%2Floyalties in the browser, I get a
404 error as well, as it seems like the server cannot convert the %2F
to a slash.

Any thoughts? Not trying to hijack Iris/Will, but I believe this is
the same problem. If not, I'll start another thread.

Thanks,
Jeff


On May 9, 10:35 am, "iris.wonderbra iris.wonderbra"
<iris.wonder...@googlemail.com> wrote:
> Hi Bruce,
>
> Appreciate it - have tried both of these approaches but no luck. When
> I have "http://api.klout.com/v2/user.json/" in the library as the URL
> and pass the query "<QUERY>/score?key=<KEY>" the API grumbles that I
> haven't supplied the key. And when I pass the entire URL as the query
> (with the library URL set to "") I get an IXMLHTTPREQUEST failed.
>
> It seems like the URL is being called before the query is appended to
> it for some reason, and therefore I can't have a partial library URL
> and pass the rest via the query. Is there a simply way of defining the
> URL here such that the query is appended to its middle rather than the
> end given how you've written the rest of the code?
>
>  With .add("klout score")
>             .add "restType", erQueryPerRow
>             .add "url", "http://api.klout.com/v2/user.json/<QUERY>/
> score?key=<KEY>"
>             .add "results"
>             .add "treeSearch", True
>             .add "ignore", vbNullString
>         End With
>
> Thanks for the tips Bruce.
>
> Will
>
> On May 9, 4:19 pm, Bruce Mcpherson <br...@mcpher.com> wrote:
>
>
>
>
>
>
>
> > Will
>
> > ok .. a couple of solutions.
>
> >    - you make the query include the key each time..
> >       -  so your url would just behttp://api.klout.com/v2/user.json/
> >       - the query parameter each time would be <QUERY>/score?
> >       key=xxxx"
> >    - you dont bother with adding your term to the library, and just passed
> >    the complete URL to the crest class
>
> > bruce
> > 2. you query parameter would include the key
>

Will Hanschell

unread,
May 11, 2012, 5:05:35 AM5/11/12
to Excel Ramblings
Thanks for the follow-ups guys! And yes - that was my alter-ego, made
the mistake of posting while logged in to work G accounts - still a
n00b at heart. (Apologies for confusion and for having to remove the
post)

Jeff - this is the exact problem I'm having, happy to share the
thread.

Unless we can find a way to pass part of the URL as the query from a
cell without symbols becoming garbled, my instinct is that we'll need
to adjust how the URL & query are concatenated elsewhere in the code

Will

Bruce Mcpherson

unread,
May 11, 2012, 10:44:22 AM5/11/12
to excel-r...@googlegroups.com
Hi Jeff

good catch... this is probably whats happening to Will as well.. What happens is that queries are URI encoded .. special characters converted to HEX - this is the standard for queries passed in a URL command line. Because it thinks that anything passed is in fact a query (rather than part of a URL) it converts that too.

I'll see if I can figure out a way that can do this without it all being too clumsy. Thanks

bruce

bruce

unread,
May 11, 2012, 11:19:02 AM5/11/12
to excel-r...@googlegroups.com
Will & Jeff

I think we can take care of this by tweaking how the query encode URI is handled. Since I cant access your URL's I need you try this for me.
Replace the encdodedUri property in the crest class with the code below. This will avoid the 'non query' part of the query string to avoid encoding. Let me know if it works and i'll build it into the master.

Bruce

Public Property Get encodedUri() As String
    Dim p As Long, s As String
    s = pRestUrlStem
' sometimes a query can contain stuff not to be encoded.. we identify this by the presence of = in the query string
' and encode after that
    p = InStrRev(pQueryString, "=")
    If (p > 0) Then
        s = s + left(pQueryString, p)
        If (p < Len(pQueryString)) Then s = s + URLEncode(Mid(pQueryString, p + 1))
    Else
        s = s + URLEncode(pQueryString)
    End If
    encodedUri = s
End Property
> > > > > To post to this group, send email to excel-ramblings@googlegroups.com.

> > > > > To unsubscribe from this group, send email to

> > > > > For more options, visit this group at
> > > > >http://groups.google.com/group/excel-ramblings?hl=en.
>
> > > --
> > > You received this message because you are subscribed to the Google Groups
> > > "Excel Ramblings" group.
> > > To post to this group, send email to excel-ramblings@googlegroups.com.

> > > To unsubscribe from this group, send email to

> > > For more options, visit this group at
> > >http://groups.google.com/group/excel-ramblings?hl=en.

--
You received this message because you are subscribed to the Google Groups "Excel Ramblings" group.
To post to this group, send email to excel-ramblings@googlegroups.com.
To unsubscribe from this group, send email to excel-ramblings+unsubscribe@googlegroups.com.

Jeff Stoddard

unread,
May 11, 2012, 12:16:34 PM5/11/12
to excel-r...@googlegroups.com
Bruce - first let me say thank you for all your help. Your site is an amazing resource. Apologies for not thanking you in my first email.

If I am following the code correctly, I think this will likely work for Will's case, but I don't think it will work for my case as the URLs are formatted differently.

In my URL https://nectar.xyz.com/merchants/189/loyalties there is no equals signs to tell the encoder not to encode. I am using 189/loyalties as my query string. In fact, I only need to vary the 189 part, but adding /loyalties to the end of the URL gives me the set of data that I am looking for. For what I have, /merchants/189 gives me data about merchant #189, /merchants/189/loyalties gives me information about merchant 189's loyalty program

When I put in your suggested changes, I still get the same 404 error saying it cannot find /merchants/189%2Floyalties

I also tried changing the code in cRest to look for a slash by changing

 p = InStrRev(pQueryString, "=") to  p = InStrRev(pQueryString, "/")

but when I did this I did not get an error, but also my data did not populate like I would have expected. This could be user error on my part, so I will keep trying. Do you think simply changing the "=" to a "/" would work in my instance?

Thanks for all your help,
Jeff

To view this discussion on the web visit https://groups.google.com/d/msg/excel-ramblings/-/2unZPMuSXy4J.

To post to this group, send email to excel-r...@googlegroups.com.
To unsubscribe from this group, send email to excel-ramblin...@googlegroups.com.

Bruce Mcpherson

unread,
May 11, 2012, 2:25:10 PM5/11/12
to excel-r...@googlegroups.com
Jeff

actually I think we could test  either / or = to cover both cases but would only work in the case where the query was the last substitutable in other words

/x/y/query/a expressed as query/a   would work

but x/y/query/a/b expressed as query/a/b would not

let me think about how to generalize it a bit more.

I cant test it since i dont have security access to your site but i think it should work.

- the population of data is independent of how the query is made so you must have some other issue with that. btw you can use http://ramblings.mcpher.com/Home/excelquirks/json/restexplore  to play around with queries and see what gets returned.(its in the restlibrary form of cdatasel.xlsm)

bruce

Jeff Stoddard

unread,
May 11, 2012, 3:47:56 PM5/11/12
to excel-r...@googlegroups.com
thanks Bruce - 

thanks to your help, I've got it all working fine. Had a small hiccup with data that I thought was being returned as one row as actually multiple rows, but it seems like when I set treesearch to true it is just picking up the first row, which is what I need

I appreciated all your effort and your personal help!

Thanks
Jeff

Bruce Mcpherson

unread,
May 11, 2012, 4:16:19 PM5/11/12
to excel-r...@googlegroups.com
very good. . good luck with all that. Will post here when I've figured out a generic solution for your scenario and Will's

bruce

will.ha...@iris-worldwide.com

unread,
May 17, 2012, 11:33:14 AM5/17/12
to Excel Ramblings
Sorry to just catch up on this.

Bruce - your fix worked perfectly for me and has fixed the whole
problem.

Your site and assistance has been brilliant, I'll recommend it as a
resource in the future.

Thanks
Will

On May 11, 9:16 pm, Bruce Mcpherson <br...@mcpher.com> wrote:
> very good. . good luck with all that. Will post here when I've figured out
> a generic solution for your scenario and Will's
>
> bruce
>
> >> On 11 May 2012 11:16, Jeff Stoddard <jeffstodd...@gmail.com> wrote:
>
> >>> Bruce - first let me say thank you for all your help. Your site is an
> >>> amazing resource. Apologies for not thanking you in my first email.
>
> >>> If I am following the code correctly, I think this will likely work for
> >>> Will's case, but I don't think it will work for my case as the URLs are
> >>> formatted differently.
>
> >>> In my URLhttps://nectar.xyz.com/merchants/189/loyaltiesthere is no
> >>>>> On 10 May 2012 19:11, Jeff Stoddard <jeffstodd...@gmail.com> wrote:
>
> >>>>>> Iris (wonderbra? - are you an alter ego of Will?) & Bruce -
>
> >>>>>> Perhaps I can shed some light as I'm having a similar problem. I need
> >>>>>> to construct a URL with the query in the middle as well.
>
> >>>>>> my format is: xyz.com/merchant/<ID>/**loyalties which if I simply
> >>>>>> type
> >>>>>> in xyz.com/merchant/189/loyalties in a browser spits back JSON to me
>
> >>>>>> I can enter all this as a URL in the restLibrary and it will work
> >>>>>> fine, but to get it to work, I have to hard code the merchant ID by
> >>>>>> using "xyz.com/merchant/189/**loyalties<http://xyz.com/merchant/189/loyalties>"
> >>>>>> as the URL. Of course, I'd like
> >>>>>> to be able to change the merchant ID by row.
>
> >>>>>> If I try to put 189/loyalties as the parameter it does not work as the
> >>>>>> slash is converted to "%2F" and it gives me a 404 error
>
> >>>>>> If I enter xyz.com/merchant/189%**2Floyalties<http://xyz.com/merchant/189%2Floyalties>in the browser, I get a
> >>>>>> 404 error as well, as it seems like the server cannot convert the %2F
> >>>>>> to a slash.
>
> >>>>>> Any thoughts? Not trying to hijack Iris/Will, but I believe this is
> >>>>>> the same problem. If not, I'll start another thread.
>
> >>>>>> Thanks,
> >>>>>> Jeff
>
> >>>>>> On May 9, 10:35 am, "iris.wonderbra iris.wonderbra"
> >>>>>> <iris.wonder...@googlemail.com**> wrote:
> >>>>>> > Hi Bruce,
>
> >>>>>> > Appreciate it - have tried both of these approaches but no luck.
> >>>>>> When
> >>>>>> > I have "http://api.klout.com/v2/user.**json/<http://api.klout.com/v2/user.json/>"
> >>>>>> in the library as the URL
> >>>>>> > and pass the query "<QUERY>/score?key=<KEY>" the API grumbles that I
> >>>>>> > haven't supplied the key. And when I pass the entire URL as the
> >>>>>> query
> >>>>>> > (with the library URL set to "") I get an IXMLHTTPREQUEST failed.
>
> >>>>>> > It seems like the URL is being called before the query is appended
> >>>>>> to
> >>>>>> > it for some reason, and therefore I can't have a partial library URL
> >>>>>> > and pass the rest via the query. Is there a simply way of defining
> >>>>>> the
> >>>>>> > URL here such that the query is appended to its middle rather than
> >>>>>> the
> >>>>>> > end given how you've written the rest of the code?
>
> >>>>>> >  With .add("klout score")
> >>>>>> >             .add "restType", erQueryPerRow
> >>>>>> >             .add "url", "http://api.klout.com/v2/user.**json/<http://api.klout.com/v2/user.json/>
> >>>>>> <QUERY>/
> >>>>>> > score?key=<KEY>"
> >>>>>> >             .add "results"
> >>>>>> >             .add "treeSearch", True
> >>>>>> >             .add "ignore", vbNullString
> >>>>>> >         End With
>
> >>>>>> > Thanks for the tips Bruce.
>
> >>>>>> > Will
>
> >>>>>> > On May 9, 4:19 pm, Bruce Mcpherson <br...@mcpher.com> wrote:
>
> >>>>>> > > Will
>
> >>>>>> > > ok .. a couple of solutions.
>
> >>>>>> > >    - you make the query include the key each time..
> >>>>>> > >       -  so your url would just behttp://api.klout.com/v2/**
> >>>>>> user.json/ <http://api.klout.com/v2/user.json/>
> >>>>>> > >       - the query parameter each time would be <QUERY>/score?
> >>>>>> > >       key=xxxx"
> >>>>>> > >    - you dont bother with adding your term to the library, and
> >>>>>> just passed
> >>>>>> > >    the complete URL to the crest class
>
> >>>>>> > > bruce
> >>>>>> > > 2. you query parameter would include the key
>
> >>>>>> > > On 9 May 2012 03:24, Will Hanschell <will.hansch...@gmail.com>
> >>>>>> wrote:
>
> >>>>>> > > > Brilliant Bruce, thank you - this is such a great resource.
>
> >>>>>> > > > Your advice makes total sense and works perfectly for the URL I
> >>>>>> > > > supplied. However, my issue still applies for the second call I
> >>>>>> need
> >>>>>> > > > to make.
>
> >>>>>> > > > Klout requires that you call their API once to convert a Twitter
> >>>>>> > > > screenName (the rows in my spreadsheet) to a Klout ID. (This is
> >>>>>> what
> >>>>>> > > > you helped me with before.)
> >>>>>> > > > You then call the API again to lookup a Klout score for that
> >>>>>> Klout ID.
> >>>>>> > > > The URL for that is "http://api.klout.com/v2/user.**json/<http://api.klout.com/v2/user.json/>
> >>>>>> <QUERY>/score?
> >>>>>> > > > key=xxxx" where <QUERY> is the query supplied, and here it's
> >>>>>> > > > hierarchical I believe so I can't just swap the terms?
>
> >>>>>> > > > These are probably very simple questions but this is the final
> >>>>>> step in
> >>>>>> > > > this hack! So close!
>
> >>>>>> > > > Will
>
> >>>>>> > > > On May 9, 12:06 am, Bruce Mcpherson <br...@mcpher.com> wrote:
> >>>>>> > > > > Will
>
> >>>>>> > > > > I dont know if you noticed the rest explorer... you can use
> >>>>>> this to play
> >>>>>> > > > > around with what your rest query returns, as well as play
> >>>>>> around with the
> >>>>>> > > > > parameters and URL.
>
> >>>>>> > > > >http://ramblings.mcpher.com/**Home/excelquirks/json/**
> >>>>>> restexplore<http://ramblings.mcpher.com/Home/excelquirks/json/restexplore>
>
> >>>>>> > > > > As I mentioned,
>
> ...
>
> read more »
Reply all
Reply to author
Forward
0 new messages