我已经开发了访问 bit.ly 缩短URL的ASP.NET MVC Web应用程序。我用的是.NET的NuGet库 Bitly.Net 因为它被称为在Bit.ly code参考。
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.
我所有的本地机器上运行良好,无论是在单元测试时,我跑了本地的Web应用程序。然而,当我部署它到Azure失败。不幸的是Bitly.Net没有给人一种有用的错误反馈,但由于这个SO回答表明,它很容易写你自己的,所以我做到了。
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.
在调用缩短命令我得到的500错误code现在和错误文本说,无效登录。
Now on calling the shorten command I get an error code of 500 and the error text says 'INVALID LOGIN'.
我使用的是德preciated身份验证方法,但看着就缩短是不说,我可以使用该身份验证方法,它工作在本地所以它不是这样。我可以换到新的OAuth认证,但我认为这将有同样的问题。
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.
有没有人访问来自Azure中bit.ly API?可以 CORS 是一个问题?任何建议感激地接受。
Has anyone accessed the bit.ly API from Azure? Could CORS be an issue?? Any suggestions gratefully received.
推荐答案好吧,其实我是通过电子邮件发送bit.ly API支持(api@bitly),他们亲切地回来了,他们说:
Ok, I actually emailed bit.ly api support (api@bitly) and they kindly came back and they said:
这是INVALID_LOGIN错误表示要么你提供了不正确的登录或不正确的apiKey参数。
An INVALID_LOGIN error indicates either you provided an incorrect ‘login’ or incorrect ‘apiKey’ parameter.
什么是奇怪的是我已经通过记录他们从下载Azure中的web.config检查登录和apiKey参数。然而,bit.ly suppport人建议我用href=\"dev.bitly/authentication.html\" rel=\"nofollow\">固定的OAuth令牌一个
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).
这让你获得一个令牌一次,然后在您所有的要求,这使得使用OAuth易于使用。 我换到的OAuth和它的工作!
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!
在此情况下,是有人在这里有用的是code,based在@devfunkd实施但更新为:
In case this is useful to someone here is the code, based on @devfunkd implementation but updated to:
- 使用固定的OAuth令牌进行验证。
- 使用bit.ly的V3 API,它有一个更好的JSON格式。
- 它使用Json.NET JSON解串器。
- 我做到了异步。
请注意,在code字段 _bitlyToken 应该包含去的此页面。在 _logger 变量保存记录,以便记录错误。
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 stackoverflow/questions/31487902/nuget-package-for-bitly-to-shorten-the-links var url = string.Format("api-ssl.bitly/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.
更多推荐
从Azure的网站访问时,Bit.ly的无效登录
发布评论