Sharepoint authentication in C# not working - c#

I'm trying to automate some menial tasks on sharepoint by creating C# code. I think the code is correct, however when running it, I get a 401 error stating that "The remote server returned an error: (401) Unauthorized."
The credentials of the user Im using in the code has full control access over the list in question, and can perform the tasks in the code through the UI. This is the first time im programming C# so any help would be greatly appreciated!
using System;
using System.Net;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
namespace ConsoleApplication3
{
class Program
{
static void Main()
{
ClientContext context = new ClientContext("websiteURL");
context.Credentials = new NetworkCredential("user", "pasword", "domain");
List oList = context.Web.Lists.GetByTitle("TestBI");
ListItem oListItem = oList.GetItemById(0);
for (int i = 0; i < 3; i++)
{
oListItem = oList.GetItemById(i);
oListItem.BreakRoleInheritance(true, false);
}
context.ExecuteQuery();
}
}
}

Since you're not using a domain account, so the way to create a NetworkCredential should be:
context.Credentials = new NetworkCredential("user", "pasword");

Related

C# ScrapySharp 'System.Net.CookieException: 'The 'Name'='HttpOnly, NID' part of the cookie is invalid.'

So i'm facing an unexpected issue with my code. For some reason, I am unable to download & print the links out of my Google search... Help is much appreciated as I'm really not sure what is going on here... I am also using the DotNET SDK
using System;
using System.Threading.Tasks;
using ScrapySharp;
using ScrapySharp.Extensions;
using ScrapySharp.Network;
using static System.Console;
namespace Test
{
class Program
{
static async Task Main(string[] args)
{
var query = "scrapysharp";
Console.WriteLine($"Searching '{query}' on google");
var browser = new ScrapingBrowser();
browser.UseDefaultCookiesParser = false;
var resultsPage = await browser.NavigateToPageAsync(new Uri($"https://www.google.fr/search?q={query}"));
Console.WriteLine($"Results");
foreach (var link in resultsPage.Html.CssSelect("h3.r a"))
{
Console.WriteLine($"- {link.InnerText}");
}
}
}
Error:
System.Net.CookieException: 'The 'Name'='HttpOnly, NID' part of the cookie is invalid.'
I was facing the same issue, the quick workaround for me was bellow one line code.
browser.IgnoreCookies = true;
Leave everything else as is, add this line after the line where you are creating browser object and try it out.

C# tweetinvi code 408

i am trying to learn connecting C# to Twitter by using tweetinvi.
i have no problem while connecting with Twitter Key and Twitter Token
then i debug my code, noticed null value on User.GetAuthenticatedUser()
however, i'm already authorize the twitter apps with my own twitter account.
Why does User.GetAuthenticatedUser() return Null Value ?
i got the following picture while trying to pass the error into Message Box
how do i resolve this ?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tweetinvi;
namespace Twitdesk
{
public partial class Form1 : Form
{
Cl_Tweetinvi twitinvi;
Command cmd = new Command();
public Form1()
{
InitializeComponent();
twitinvi = new Cl_Tweetinvi();
var AuthenticatedUser = User.GetAuthenticatedUser();
if(AuthenticatedUser == null)
{
var latestException = ExceptionHandler.GetLastException();
MessageBox.Show(latestException.ToString());
Application.Exit();
}
else
{
var settings = AuthenticatedUser.GetAccountSettings();
}
var tweets = Timeline.GetHomeTimeline();
this.Text = cmd.title;
MessageBox.Show("done");
}
}
}
The problem comes from the fact that you have not initialized your credentials.
You need to call Auth.SetUserCredentials("CONSUMER_KEY", "CONSUMER_SECRET", "ACCESS_TOKEN", "ACCESS_TOKEN_SECRET"); before performing any operation.
When you have invoked this line any operation will be using these credentials.
Please take a quick look at the wiki for more information. Or let me know if you still encounter any problem.
Though the problem could be different as the error message seems to indicate that you have a timeout problem. Normally authentication problems return 401 exception.

Lucene.Net Query Error

I've just installed Solr/Lucene on a Windows machine simply to test its capability. I've indexed a couple hundred files and I'm able to successfully query the indexed content through the web interface. Given that the web interface isn't too user friendly I thought I'd try to create a simple application to do a full text search over the indexed content. So far I have:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Lucene.Net;
namespace solrTest
{
class Program
{
static void Main(string[] args)
{
string indexFileLocation = #"C:\solr-5.3.1\server\solr\test\data\index";
Lucene.Net.Store.Directory dir = Lucene.Net.Store.FSDirectory.Open(indexFileLocation);
Lucene.Net.Search.IndexSearcher searcher = new Lucene.Net.Search.IndexSearcher(dir);
Lucene.Net.Index.Term searchTerm = new Lucene.Net.Index.Term("ID", "1");
Lucene.Net.Search.Query query = new Lucene.Net.Search.TermQuery(searchTerm);
Lucene.Net.Search.TopDocs results = searcher.Search(query, null, 10);
foreach(Lucene.Net.Search.ScoreDoc test in results.ScoreDocs)
{
Console.WriteLine(test.ToString());
Console.ReadLine();
}
}
}
}
When running the code I receive an error stating:
An unhandled exception of type 'System.IO.IOException' occurred in Lucene.Net.dll
Additional information: read past EOF
I know I'm missing something obvious. Your help would be greatly appreciated.
Edit: I downloaded Luke.Net and received the same error.

How to Generate Text File from API Source?

I have a small problem. I just recently started using Twilio's API to generate a record of messages that was sent to my assigned SID and Auth Token. However my question is how can I generate a text file, based off of what the console writes from the source its addressed to?
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using Twilio;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// Find your Account Sid and Auth Token at twilio.com/user/account
string AccountSid = "X";
string AuthToken = "X";
var twilio = new TwilioRestClient(AccountSid, AuthToken);
// Build the parameters
var options = new MessageListRequest();
options.From = "2015-07-01";
options.To = "2015-07-13";
var messages = twilio.ListMessages(options);
foreach (var message in messages.Messages)
{
Console.WriteLine(message.Body);
Console.Read();
}
}
}
}
Writing to a text file is pretty much boilerplate. The methods are shown here:
https://msdn.microsoft.com/en-us/library/8bh11f1k.aspx

RallyApi.Net Getting Workspaces from a Subscription Version 2.0

Following this link How to obtain a list of workspaces using Rally REST .NET
I tried the example however when I try to query against sub["Workspaces"] I get the error
RuntimeBinderException was unhandled;
The best overloaded method match for 'Rally.RestApi.RallyRestApi.Query(Rally.RestApi.Request)' has some invalid arguments
I cannot find any other ways to gather a list of workspaces from the subscription using the RallyApi dll for .Net which I obtained from the link provided.
Any help will be much appreciated.
Try to modify that code as follows:
Request wRequest = new Request(sub["Workspaces"]);
QueryResult queryResult = restApi.Query(wRequest);
Here is an entire app:
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using Rally.RestApi;
using Rally.RestApi.Response;
namespace Rest_v2._0_test
{
class Program
{
static void Main(string[] args)
{
//Initialize the REST API
RallyRestApi restApi;
restApi = new RallyRestApi("user#co.com", "secret", "https://rally1.rallydev.com", "v2.0");
//get the current subscription
DynamicJsonObject sub = restApi.GetSubscription("Workspaces");
Request wRequest = new Request(sub["Workspaces"]);
//query the Workspaces collection
QueryResult queryResult = restApi.Query(wRequest);
foreach (var result in queryResult.Results)
{
var workspaceReference = result["_ref"];
var workspaceName = result["Name"];
Console.WriteLine( workspaceName + " " + workspaceReference);
}
}
}
}

Categories

Resources