问题
I have developed a ASP.NET MVC web application that accesses bit.ly to shorten a url. I used the .NET NuGet library Bitly.Net as it was referred to in the Bit.ly Code Reference.
All worked fine on my local machine, both in Unit Tests and when I ran the Web Application locally. However when I deployed it to Azure it failed. Unfortunately Bitly.Net did not giving a useful error feedback, but as this SO answer showed it is easy to write you own, so I did.
Now on calling the shorten command I get an error code of 500 and the error text says 'INVALID LOGIN'.
I am using the depreciated authentication method but looking at the documentation on shorten is does say I can use that authentication method, and it works locally so its not that. I could swap over to the new OAuth authentication but I think it would have the same problem.
Has anyone accessed the bit.ly API from Azure? Could CORS be an issue?? Any suggestions gratefully received.
回答1:
Ok, I actually emailed bit.ly api support (api@bitly.com) and they kindly came back and they said:
An INVALID_LOGIN error indicates either you provided an incorrect ‘login’ or incorrect ‘apiKey’ parameter.
What was weird is I had checked the ‘login’ and ‘apiKey’ parameter by logging them and downloading the web.config from Azure. However the bit.ly suppport person suggested I used a fixed OAuth Token (read the top line that is in bold on the linked page for more about this).
This allows you to get a Token once and then use it in all your requests, which makes using OAuth easy. I swapped over to OAuth and it worked!
In case this is useful to someone here is the code, based on @devfunkd implementation but updated to:
- Use the fixed OAuth token for validation.
- Use the bit.ly's V3 API, which has a nicer json format.
- It uses Json.NET json deserialiser.
- I made it async.
Note that in the code the field _bitlyToken
should contain a token created by going to this page. The _logger
variable holds a logger so that errors are logged.
public async Task<string> ShortenAsync(string longUrl)
{
//with thanks to @devfunkd - see https://stackoverflow.com/questions/31487902/nuget-package-for-bitly-to-shorten-the-links
var url = string.Format("https://api-ssl.bitly.com/v3/shorten?access_token={0}&longUrl={1}",
_bitlyToken, HttpUtility.UrlEncode(longUrl));
var request = (HttpWebRequest) WebRequest.Create(url);
try
{
var response = await request.GetResponseAsync();
using (var responseStream = response.GetResponseStream())
{
var reader = new StreamReader(responseStream, Encoding.UTF8);
var jsonResponse = JObject.Parse(await reader.ReadToEndAsync());
var statusCode = jsonResponse["status_code"].Value<int>();
if (statusCode == (int) HttpStatusCode.OK)
return jsonResponse["data"]["url"].Value<string>();
//else some sort of problem
_logger.ErrorFormat("Bitly request returned error code {0}, status text '{1}' on longUrl = {2}",
statusCode, jsonResponse["status_txt"].Value<string>(), longUrl);
//What to do if it goes wrong? I return the original long url
return longUrl;
}
}
catch (WebException ex)
{
var errorResponse = ex.Response;
using (var responseStream = errorResponse.GetResponseStream())
{
var reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
var errorText = reader.ReadToEnd();
// log errorText
_logger.ErrorFormat("Bitly access threw an exception {0} on url {1}. Content = {2}", ex.Message, url, errorText);
}
//What to do if it goes wrong? I return the original long url
return longUrl;
}
}
I hope that helps someone.
来源:https://stackoverflow.com/questions/32741316/bit-ly-invalid-login-when-accessing-from-azure-web-site