I am trying to display a list of my videos from my YouTube channel in C#, but I can't.
When I run the program, I get such an error:
Object reference not set to an instance of an object.
YouTubeRequestSettings settings = new YouTubeRequestSettings("my app name",
"my devkey",
"my email", "my password");
YouTubeRequest request = new YouTubeRequest(settings);
string feedUrl = "https://gdata.youtube.com/feeds/api/users/default/uploads";
Feed<Video> videoFeed = request.Get<Video>(new Uri(feedUrl));
foreach (Video entry in videofeed.Entries)
{
list.Items.Add(entry);
}
Any help will be appreciated.
Looks like it should be
YouTubeRequest settings = new YouTubeRequest(ayarlar);
otherwise you're trying to pass the not-yet instantiated settings variable as a parameter to the YouTubeRequest constructor.
Update from comment
Since you want a global variable, don't create a new instance here and use the existing one like this
//earlier code
string feedUrl = "https://gdata.youtube.com/feeds/api/users/default/uploads";
//the Feed<Video> has been removed because you want to use the global variable
videoFeed = request.Get<Video>(new Uri(feedUrl));
foreach (Video entry in videofeed.Entries)
{
list.Items.Add(entry);
}
Related
I can't see the name of the tables already created. I'm working on a project in which I have access to the DynamoDB database through an IAM client, I create the AmazonClient using the credentials and configs that were made available to me, but I can't see the tables already created in the database.
I have already created the client and connected it to the database, I am trying to see the number of tables as follows, but the result is always 0
new code
List<string> currentTables = client.ListTablesAsync().Result.TableNames;
MessageBox.Show(currentTables.Count.ToString());
Try awaiting the API call:
List<string> currentTables = await client.ListTablesAsync().Result.TableNames;
MessageBox.Show(currentTables.Count.ToString());
Try this sync code instead:
AmazonDynamoDBClient client = new AmazonDynamoDBClient();
// Initial value for the first page of table names.
string lastEvaluatedTableName = null;
do
{
// Create a request object to specify optional parameters.
var request = new ListTablesRequest
{
Limit = 10, // Page size.
ExclusiveStartTableName = lastEvaluatedTableName
};
var response = client.ListTables(request);
ListTablesResult result = response.ListTablesResult;
foreach (string name in result.TableNames)
Console.WriteLine(name);
lastEvaluatedTableName = result.LastEvaluatedTableName;
} while (lastEvaluatedTableName != null);
When programmatically creating a Cognito user pool and app client, if the app client is to have read/write access to attributes of the user pool, that access must be explicitly given. I have been able to do so successfully for custom attributes but built-in attributes always return an error of "Invalid write attributes specified while creating a client" or "Invalid read attributes specified while creating a client".
Documentation is ... both voluminous and difficult to find. I have yet to see an example of this or an actual bit of useful documentation on the CreateUserPoolClientRequest type that says anything about this other than things like "ReadAttributes is a list of strings that are the attributes that can be read".
Here is the code I'm using that always ends up with that error message and failure to create the app client. _client is an AmazonCognitoIdentityProviderClient properly instantiated and credentialed and running in a lambda function.
var request = new CreateUserPoolClientRequest { UserPoolId = userPoolId, ClientName = $"{name}AppClient" };
var builtInAttributes = new List<string>()
{
"address","birthdate","email","family name","gender","given name","locale","middle name","name","nickname","phone number", "picture","preferred username","profile","zoneinfo","updated at","website"
};
var readAttributes = new List<string>();
var writeAttributes = new List<string>();
readAttributes.InsertRange(0,builtInAttributes);
writeAttributes.InsertRange(0, builtInAttributes);
var attributeConfig = ConfigurationHelper.GetListFromSection("UserPoolCustomAttributes");
foreach (var attribute in attributeConfig)
{
readAttributes.Add($"custom:{attribute.Key}");
writeAttributes.Add($"custom:{attribute.Key}");
}
request.ReadAttributes = readAttributes;
request.WriteAttributes = writeAttributes;
var result = await _client.CreateUserPoolClientAsync(request, CancellationToken.None);
Any help is greatly appreciated.
Figured it out. Though I have yet to find it documented anywhere, default attributes with a space in the name in the ui need to have that space replaced with an underscore when using their name in the api.
I have the following code:
UserActivitySession _currentActivity;
private async Task GenerateActivityAsync()
{
// Get the default UserActivityChannel and query it for our UserActivity. If the activity doesn't exist, one is created.
UserActivityChannel channel = UserActivityChannel.GetDefault();
UserActivity userActivity = await channel.GetOrCreateUserActivityAsync("Test");
// Populate required properties
userActivity.VisualElements.DisplayText = ActivityName.Text; //ActivityName is textbox
userActivity.ActivationUri = new Uri(ActivityUri.Text); //ActivityUri is also textbox
userActivity.VisualElements.Description = "This is test item";
userActivity.VisualElements.Attribution =
new UserActivityAttribution(new Uri("http://via.placeholder.com/16x16"))
{ AlternateText = "Test123" };
//Save
await userActivity.SaveAsync(); //save the new metadata
// Dispose of any current UserActivitySession, and create a new one.
_currentActivity?.Dispose();
_currentActivity = userActivity.CreateSession();
}
And it results in something like on the screenshot: https://imgur.com/a/LK0NHAa
I want my app's name to be displayed after the dash character (the dash is inserted there by Windows).
You code specifies this:
userActivity.VisualElements.DisplayText = ActivityName.Text;
tried changing the DisplayText to the value you want?
Getting app name:
var pack = Package.Current.Id.Name;
I have 2 Sharepoint 2013 sites.
When user adding new item in SPList at first SPSite -> starting workflow, what must added copy of item in SPList at second SPSite. This is my code:
public void UpdateSPList(string Title)
{
using (AuthenticationSvc.Authentication authSvc = new AuthenticationSvc.Authentication())
{
try
{
using (ListsSvc.Lists list = new ListsSvc.Lists())
{
list.Url = #"http://second-srharepoint-site.com/_vti_bin/Lists.asmx";
list.CookieContainer = new System.Net.CookieContainer();
list.AllowAutoRedirect = true;
list.PreAuthenticate = true;
list.Credentials = new System.Net.NetworkCredential("domain\\username", "password");
string strBatch = "<Method Cmd='New'><Field Name='Title'>" + Title + "</Field> ";
XmlDocument xmlDoc = new XmlDocument();
XmlElement elBatch = xmlDoc.CreateElement("Batch");
elBatch.SetAttribute("OnError", "Continue");
elBatch.InnerXml = strBatch;
XmlNode ndReturn = list.UpdateListItems("SPListName", elBatch);
}
}
finally
{
}
}
}
But on line elBatch.InnerXml = strBatch; I get exception:
$exception {"Unexpected end of file has occurred. The following elements are not closed: Method. Line 1, position 60."}
System.Exception {System.Xml.XmlException}
I don't know how fix this problem. Help me, please.
First, the string isn't valid XML because the closing Method element is missing. It should be
"<Method Cmd='New'><Field Name='Title'>" + Title + "</Field></Method>"
Second, the ASMX services were deprecated back in 2010. You shouldn't use them for any king of development, especially against SP 2013. The client-side object model (CSOM) is a lot simpler and easier to use. There are a lot of examples in the documentation. The snippet that creates a new item is :
// Starting with ClientContext, the constructor requires a URL to the
// server running SharePoint.
ClientContext context = new ClientContext("http://SiteUrl");
// Assume that the web has a list named "Announcements".
List announcementsList = context.Web.Lists.GetByTitle("Announcements");
// We are just creating a regular list item, so we don't need to
// set any properties. If we wanted to create a new folder, for
// example, we would have to set properties such as
// UnderlyingObjectType to FileSystemObjectType.Folder.
ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
ListItem newItem = announcementsList.AddItem(itemCreateInfo);
newItem["Title"] = "My New Item!";
newItem["Body"] = "Hello World!";
newItem.Update();
context.ExecuteQuery();
No XML fiddling, you simply create a new item, set its properties and call Update
I want to delete videos from my YouTube channel which video ids are selected, though MultiSelection property of ListBox is on, code doesn't work, is there any other solution? I get such an error as follows:
Execution of request failed: http://gdata.youtube.com/feeds/api/users/xxxxxx/uploads/System.Windows.Forms.ListBox+SelectedObjectCollection
Here is my code:
public void delete()
{
YouTubeRequestSettings settings = new YouTubeRequestSettings(my app name,
my dev key,
my username,
my password);
YouTubeRequest request = new YouTubeRequest(settings);
Uri videoEntryUrl = new Uri(String.Format("http://gdata.youtube.com/feeds/api/users/{0}/uploads/{1}", my channel name, list.checkedItems));
Video video = request.Retrieve<Video>(videoEntryUrl);
request.Delete(video);
}
Code for Populating the CheckedListBox
Feed<Video> videoFeed;
string feedUrl = "https://gdata.youtube.com/feeds/api/users/default/uploads";
videoFeed = request.Get<Video>(new Uri(feedUrl));
foreach (Video entry in videoFeed.Entries)
{
list.Items.Add(entry.VideoId,0);
}
Ok...I think the key here is to get the data out of the object collection - and an easy way to do that is with a foreach loop. I'm not familiar with the YouTube API so I don't know what format it expects the video ID in (for multiple videos), but for purposes of this example I'll use a comma.
string videoIDs = "";
foreach (object vidID in list.CheckedItems)
{
videoIDs = videoIDs + vidID.ToString() + ",";
}
videoIDs = videoIDs.Substring(0, videoIDs.Length - 1);
Basically, the above code loops through the CheckedListBox.CheckedItemCollection and gets the video IDs, which is what you are storing in the CheckedBoxList from the code you provided.
Then you can simply use the videoIDs string in your code:
Uri videoEntryUrl = new Uri(String.Format("http://gdata.youtube.com/feeds/api/users/{0}/uploads/{1}", my channel name, videoIDs));
Again, this is a general approach - you will need to modify the code to fit the YouTube API.
Feed<Video> videoFeed;
string feedUrl = "https://gdata.youtube.com/feeds/api/users/default/uploads";
videoFeed = request.Get<Video>(new Uri(feedUrl));
foreach (Video entry in videoFeed.Entries)
{
list.Items.Add(entry.VideoId,0);
}