JohnTranstel / google-gdata

Automatically exported from code.google.com/p/google-gdata
0 stars 0 forks source link

.NET API SDK probably problem with API keys #541

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Hello, 
I was integrating the .NET API SDK into our website. I had to simply 
upload (from a source, not browser based upload), preview and delete 
video into our channel. Everything worked very well on my machine but 
when I deployed all that on our live server I've received some 
disturbing exceptions. No matter on what other machine different from 
my PC I deploy my code my YouTubeRequest instance throws : 
Execution of request failed: 
http://uploads.gdata.youtube.com/feeds/api/users/default/uploads 
-=Stack Trace=- : 
   at Google.GData.Client.GDataRequest.Execute() 
   at Google.GData.Client.GDataGAuthRequest.Execute(Int32 
retryCounter) 
   at Google.GData.Client.GDataGAuthRequest.Execute(Int32 
retryCounter) 
   at Google.GData.Client.GDataGAuthRequest.Execute(Int32 
retryCounter) 
   at Google.GData.Client.GDataGAuthRequest.Execute(Int32 
retryCounter) 
   at Google.GData.Client.GDataGAuthRequest.Execute() 
   at Google.GData.Client.MediaService.EntrySend(Uri feedUri, AtomBase 
baseEntry, GDataRequestType type, AsyncSendData data) 
   at Google.GData.Client.Service.Insert(Uri feedUri, AtomEntry 
newEntry, AsyncSendData data) 
   at Google.GData.Client.Service.Insert[TEntry](Uri feedUri, TEntry 
entry) 
   at Google.GData.YouTube.YouTubeService.Upload(String userName, 
YouTubeEntry entry) 
   at Google.YouTube.YouTubeRequest.Upload(String userName, Video v) 
   at Google.YouTube.YouTubeRequest.Upload(Video v) 
The inner exception is Bad Request (400). 
If case the exception to GDataRequestException i have ResposnseString 'Invalid 
Request'.
I've tried 5 different API keys generated on different machines with different 
accounts - all of them work on my machine, but no luck on others.
Here is how I'm using the API : 
private YouTubeRequest GetYouTubeRequest() 
        { 
            return new YouTubeRequest(new 
YouTubeRequestSettings(Config.YOUTUBE_APPLICATION_NAME, 
                Config.YOUTUBE_API_KEY, 
                Config.YOUTUBE_USERNAME, 
                Config.YOUTUBE_PASSWORD)); 
        } 
public bool Upload(UserDto user, string path) 
        { 
            try 
            { 
                Video newVideo = new Video(); 
                newVideo.Title = user.FirstName + "'s 
findababysitter.com profile video"; 
                newVideo.Keywords = "findababysitter.com , childcare, 
babysitter, nanny"; 
                newVideo.Media.Categories.Add(new 
MediaCategory("Autos")); 
                newVideo.Description = "Visit " + user.FirstName + "'s 
profile at http://www.findababysitter.com/profile/" + user.Id + " !"; 
                newVideo.YouTubeEntry.Private = true; 
                newVideo.YouTubeEntry.MediaSource = new 
MediaFileSource(path, 
MediaFileSource.GetContentTypeForFileName(path)); 
                Video createdVideo = 
GetYouTubeRequest().Upload(newVideo); 
                m_uvRep.Save(new tUserVideo() 
                { 
                    UserID = user.Id.Value, 
                    YouTubeID = createdVideo.VideoId 
                }); 
                return true; 
            } 
            catch (Exception ex) 
            { 
                ErrorLog.WriteExceptionLogVerbose("Error while 
uploading video for userId :" + user.Id, ex); 
                return false; 
            } 
            finally 
            { 
                FileService.DeleteFileFullPath(path); 
            } 
        } 
On the line Video createdVideo = GetYouTubeRequest().Upload(newVideo); 
I got this exception. 
My youtube details (copy from my web.config, password hidden) : 
<add key="YOUTUBE_APPLICATION_NAME" value="Findababysitter.com" /> 
      <add key="YOUTUBE_API_KEY" 
value="AI39si5DrLm2t-8FG6nvORGezDYUlB_OwRb9WKB5kA3eLBQEB0hN_qondNRiW4dlzxd3 
GhjiBlLxWUOQe1TNxcYki27yhcDoA" / 

      <add key="YOUTUBE_USERNAME" value="t...@findababysitter.com" /> 
      <add key="YOUTUBE_PASSWORD" value="*******" /> 
      <add key="YOUTUBE_CHANNEL_NAME" value="findababysitter" /> 
Can someone help me why I'm experiencing this issue? 
Thanks, 
Nikolay Stoyanov, 
www.findababysitter.com 

Original issue reported on code.google.com by mathcore...@gmail.com on 29 Sep 2011 at 10:01

GoogleCodeExporter commented 8 years ago
FOUND THE PROBLEM ! TO ALL DEVS : Do not assume that 
MediaFileSource.GetContentTypeForFileName(path) returns the mime type 
always. You have to install windows media player on the server, so the 
video file extensions can be properly inserted in the machine's 
registries. Actually this method should be removed from the API, its 
causing troubles. User this one instead (video files only) : 
public string GetMIMETypeByFileName(string filename) 
        { 
            string extension = 
filename.Substring(filename.LastIndexOf('.') + 1).ToLower(); 
            switch (extension) 
            { 
                case "wmv" : return "video/x-ms-wmv"; 
                case "mp4" : return "video/mp4"; 
                case "mov" : return "video/quicktime"; 
                case "flv" : return "video/x-flv"; 
                case "mpg" : 
                case "mpeg" : return "video/mpeg"; 
                case "3gp" : return "video/3gpp"; 
                case "mkv":  return "video/x-matroska"; 
                case "avi": 
                default: return "video/x-msvideo"; 
            } 
        } 

Original comment by mathcore...@gmail.com on 29 Sep 2011 at 3:53

GoogleCodeExporter commented 8 years ago
Thanks for the updates, Nikolay.
Would you like to submit a patch for your solution?

Original comment by ccherub...@google.com on 29 Sep 2011 at 4:04

GoogleCodeExporter commented 8 years ago
Since I'm very new to the API and this issue tracker, how I can submit a patch ?
I would like to share with the community this piece of code here :

public string GetContentType(string fileName)
        {
            string contentType = "application/octetstream";
            string ext = Path.GetExtension(fileName).ToLower();

            if (string.IsNullOrEmpty(ext))
            {
                return contentType;
            }

            RegistryKey registryKey = Registry.ClassesRoot.OpenSubKey(ext);
            if (registryKey != null && registryKey.GetValue("Content Type") != null)
            {
                contentType = registryKey.GetValue("Content Type").ToString();
                return contentType;
            }

            RegistryKey typeKey = Registry.ClassesRoot.OpenSubKey(@"MIME\Database\Content Type");

            foreach (string keyname in typeKey.GetSubKeyNames())
            {
                RegistryKey curKey = typeKey.OpenSubKey(keyname);
                if (curKey != null)
                {
                    object extension = curKey.GetValue("Extension");
                    if (extension != null)
                    {
                        if (extension.ToString().ToLower() == ext)
                        {
                            return keyname;
                        }
                    }
                }
            }

            return contentType;
        }
This is a good method for getting the MIME type for a given file extension, but 
its not working perfectly (again some applications has to be installed and do 
associations with their file extensions). So I guess the 'hardcoded' way of 
returning the MIME type is the best solution. The cases above cover only the 
video file types supported by YouTube (Have I missed any?), so we should add 
the audio file types as well.

Original comment by mathcore...@gmail.com on 29 Sep 2011 at 8:13

GoogleCodeExporter commented 8 years ago
To create a patch, you have to start from the latest version of the code from 
the repository (http://code.google.com/p/google-gdata/source/checkout) and then 
run "svn diff".

BTW, your latest code uses the Windows Registry and I don't think that's a good 
idea. First, the values you'll find in the registry won't be consistent in 
different Windows installations and second I think this excludes non-Windows 
machine.

Original comment by ccherub...@google.com on 29 Sep 2011 at 9:03

GoogleCodeExporter commented 8 years ago
I just posted the last code as example, with no intention that it is going to 
do any good to determine correctly the mime type by file extension. As I've 
said its not going to work in all cases. Will extend the function with the 
hardcoded values for all types supported with youtube and will commit it next 
week.

Original comment by mathcore...@gmail.com on 30 Sep 2011 at 7:32

GoogleCodeExporter commented 8 years ago
Marking this as obsolete as there was no activity for more than 6 months.
Feel free to reopen if there are updates

Original comment by ccherub...@google.com on 11 Apr 2012 at 11:09