I'm trying to shorten urls with bitly using the c# console, the problem is that I get the error:
Error: System.Net.WebException: The remote server returned an error: (403) Forbidden.
at System.Net.HttpWebRequest.GetResponse()
at BitlyTest.Program.Shorten(String groupId, String token, String url) in Program.cs:line 54
This is the code:
class Program{
static void Main(string[] args){
Console.WriteLine("Enter group Id:");
string groupId = Console.ReadLine();
Console.WriteLine("Enter token:");
string token = Console.ReadLine();
Console.WriteLine("Enter URL:");
string url = Console.ReadLine();
Shorten(groupId, token, url);
}
public static void Shorten(string groupId, string token, string url){
string post = "{\"group_guid\": \"" + groupId + "\", \"long_url\": \"" + url + "\"}";
string shortUrl = url;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api-ssl.bitly.com/v4/shorten");
try{
System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
request.ServicePoint.Expect100Continue = false;
request.Method = "POST";
request.ContentLength = post.Length;
request.ContentType = "application/json";
request.Headers.Add("Cache-Control", "no-cache");
request.Host = "api-ssl.bitly.com";
request.Headers.Add("Authorization", "Bearer " + token);
using (Stream requestStream = request.GetRequestStream()){
byte[] postBuffer = Encoding.ASCII.GetBytes(post);
requestStream.Write(postBuffer, 0, postBuffer.Length);
}using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) //línea con el error
{
using (Stream responseStream = response.GetResponseStream()){
using (StreamReader responseReader = new StreamReader(responseStream)){
string json = responseReader.ReadToEnd();
shortUrl = Regex.Match(json, @"""link"": ?""(?<link>[^,;]+)""").Groups["link"].Value;
}
}
}
}catch (Exception ex){
Console.WriteLine("Error: " + ex);
}
Console.WriteLine("\nShortened URL: " + shortUrl);
Console.ReadKey();
}
}
Could someone help me? I left the line in which the error appears commented out just in case. I have tried to solve the error by reading but I have not found the solution. The idea is then to be able to implement this idea in C# with MVC in a WebService. I hope you can help me, thanks.
The first thing you should get is the
Token de acceso genérico
general o.To do this, go to your Bitly account, click on the menu at the top right >
Settings > Advanced Settings > API Support > click en el enlace que dice Generic Access Tokens
.Another way is to click on the menu at the top right >
My Profile > Generic Access Tokens
Enter your password and you must generate a Generic Token. This is what you will use to connect from your app.
I leave you a place to read more about this type of applications here the documentation and find the section for
aplicaciones de acceso simple
orsingle account application
.The issue of authentication has changed a bit, you can check it here: Documentation for Simple Access Applications
How you authenticate with the Bitly API has changed with V4. Previously the Authentication Token was obtained by making a request and was known as
access_token
. V4 now requires the authentication token to be sent as part of the Authorization Header on every request.(Authorization header)
.If you can't, use a Postman-type application to verify that you are making the requests and filling the Authorization Header correctly.
I hope it has helped you.