Results 1 to 3 of 3

Thread: Goo.gl URL Shortener - The remote server returned an error: (400) Bad Request.

  1. #1
    quest2chill is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Mar 2012
    Posts
    6
    Reputation
    0

    Goo.gl URL Shortener - The remote server returned an error: (400) Bad Request.

    I have successfully connected to the Google URL Shortener service at Goo.gl and had my url shortened and returned but only using the basic service.
    I now wish to initiate a request using an API Key and have a unique shortened URL returned and logged to a specific user.

    The successful basic request is made using: request = "https://www.googleapis.com/urlshortener/v1/url"

    For the request that is failing with supplied valid key I used:

    request = "https://www.googleapis.com/urlshortener/v1/url?key={" & My.Settings.GURL_SV_APIKey & "}"

    The above evaluates to: "https://www.googleapis.com/urlshortener/v1/url?key={API key Removed}" which according to Google this is all that is required.

    Message received back:

    System.Net.WebException was unhandled
    Message=The remote server returned an error: (400) Bad Request.

    Code used:

    Dim request As String


    If My.Settings.GURL_SV_APIKey <> "" Then
    request = "https://www.googleapis.com/urlshortener/v1/url?key={" & My.Settings.GURL_SV_APIKey & "}"
    Else
    request = "https://www.googleapis.com/urlshortener/v1/url"
    End If


    Dim encoding As New UTF8Encoding


    Dim req As HttpWebRequest = CType(WebRequest.Create(request), HttpWebRequest)
    Dim byteData As Byte() = encoding.GetBytes("{'longUrl': '" & TextBox1.Text & "'}")


    req.ContentType = "application/json"
    req.ContentLength = byteData.Length
    req.KeepAlive = True
    req.Method = "POST"


    Dim stream01 As IO.Stream = req.GetRequestStream
    stream01.Write(byteData, 0, byteData.Length)


    Dim resp As HttpWebResponse = CType(req.GetResponse(), HttpWebResponse)


    Dim stream02 As IO.Stream = resp.GetResponseStream
    Dim stream As New IO.StreamReader(stream02)
    Dim work As String = stream.ReadToEnd

    Any help would really be appreciated, I am getting nowhere with this.

    Regards

  2. #2
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    15,080
    Reputation
    2802
    Use query parameter key=yourAPIKey, not key={yourAPIKey}

  3. #3
    quest2chill is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Mar 2012
    Posts
    6
    Reputation
    0
    Quote Originally Posted by JohnH View Post
    Use query parameter key=yourAPIKey, not key={yourAPIKey}
    John,

    Many thanks, Request is now being excepted and is returning the shortened URL.

    Unfortunately, this still only uses the basic shortening service as shortened URL not logged to user.

    Will have to do further research.

    Bit.ly was so much easier.

    Thanks again.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •