I have been experiencing some inconsistency with the way a web page gets downloaded using PowerShell.
This is the script:
$url2 = ("webpageurl")
$credentials = Get-Credential
$webobj = New-Object System.Net.WebClient
$webobj.credentials = $credentials
$content=$webobj.downloadstring($url2)
$content
Here webpageurl is an internal webpage that takes a different credentials other than the one with which I login in to my PC.
When I run the above script, some time the webpage gets downloaded and sometimes it just fails with the following error.
Exception calling "DownloadString" with "1" argument(s): "The remote server
returned an error: (401) Unauthorized."
At C:\Users\schandras_adm\Documents\windowspowershell\get-ndswebpage.ps1:6 char:32
+ $content=$webobj.downloadstring <<<< ($url2)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
For example, if I run this say 6 times it works, the 7th time and 8th time it may fail again, and so on.
I am just confused as to why it fails sometimes and sometimes it works without any change in the script or the credentials.
Related
I have a .NET MVC project and I'm trying to download a file from temporary folder. Here is my view code:
window.open('#Url.Action("DownloadDocument", "Controller")?fileName=' + fileName, '_blank');
And here is my controller code:
public FileResult DownloadDocument(string fileName)
{
string path = Web.Common.Properties.Settings.Default.TempDocPath + "OpenTable\\";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
return File(fileName, "application/xml", fileName + ".xml");
}
This code works on IIS and file is being downloaded. But I'm getting 500 error when trying to download it from remote server.
This site can’t be reached
The webpage at <...> might be temporarily down or it may have moved permanently to a new web address.
ERR_INVALID_RESPONSE
And the worst part is that there are some almost exact code snippets in the system which are working in both IIS and remote server.
Any suggestions what could be done?
UPDATE
Good news. When I tried postman it threw a file not found error with wrong path. It's an easy fix:
return File(Path.Combine(path, fileName), "application/xml", fileName + ".xml");
So it puzzles me... How and why it was working locally on IIS with wrong path?
You need to create your temp folder inside the root folder of your web app, for example ~/Temp, then use below code to access to that folder instead of Web.Common.Properties.Settings.Default.TempDocPath, I think this maybe just a constant.
System.Web.Hosting.HostingEnvironment.MapPath("~/Temp")
I'm trying to upload videos using https://code.google.com/p/youtube-api-samples/source/browse/samples/java/youtube-cmdline-uploadvideo-sample/src/main/java/com/google/api/services/samples/youtube/cmdline/youtube_cmdline_uploadvideo_sample/UploadVideo.java example
I've successfully registered on the youtube console and have obtained the client_secrets.json at https://code.google.com/apis/console/?api=youtube#project:340964720118:access
I have installed the secrets so my app can access them; everything is compiled well, but when the program is executed, I'me getting that strange errors:
GoogleJsonResponseException code: 400 : Invalid value for: listed is not a valid value
com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
{
"code" : 400,
"errors" : [ {
"domain" : "global",
"message" : "Invalid value for: listed is not a valid value",
"reason" : "invalid"
} ],
"message" : "Invalid value for: listed is not a valid value"
}
at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:423)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460)
I can't find out what is wrong; since the request itself is deeply encapsulated, I can't understand what is wrong with the request...
UPD.
Thanks to
System.out.println(videoInsert.getHttpContent().toString());
call inserted before the
Video returnedVideo = videoInsert.execute();
in the sample code, I got that the JSOn formed and sent to the google is
{snippet={description=Video uploaded via YouTube Data API V3 using the Java library on Sun Sep 29 20:29:30 MSK 2013, tags=[test], title=Test Upload via Java on Sun Sep 29 20:29:30 MSK 2013}, status={privacyStatus=listed}}
But actually there is still a question what is wong with this json and why it causes the error on the the server side.
UPD 2:
And no I no longer get the message above, but the upload is still impossible. I'm getting 401 Error while trying to upload the file in that lines of the example above:
System.out.println(videoInsert.getJsonContent().toString());
System.out.println(videoInsert.getHttpContent().getType());
Video returnedVideo = videoInsert.execute();
System.out.println(returnedVideo.getId());
I supposed that I haven't authorized the request to my personal data, but I actually DID it (the request was loaded to the default browser where I have accepted it and specified the account I would like to work with). However, there is no way to check that authoriation; my google account data holds only Apps and Activity log for the google+ apps; I haven't fount my app in the list. So where I can check where I'm not logged in?
IOException: 401 Unauthorized
com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:423)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460)
at ScreenRecordingExample.UploadToYoutube(ScreenRecordingExample.java:356)
Ok, I have found out that the problem appeared due to the incorrect privacyStatus value:
I had
status.setPrivacyStatus("listed");
in my code, while it seems that only public, unlisted and private are possible values
And the UPD-2 problem was resolved unfortunately in the way i don't know exactly how:
i have noticed, that in my code example there was a line
return new AuthorizationCodeInstalledApp(flow, localReceiver).authorize("user");
I have replaced 'user' with my google user name (but actually I'm not sure that it was a thing that was really needed). After that replacement the authorization attempt started again (default browser loaded the accept/decline page and there I should select one of two accounts). Since I have two accounts, the original youtube one and the common google one, and during the last attempt I have selected the google's one and got 401 error, I have selected the youtube's one this time.
so, after that the problem was resolved. I still don't know do it due to the correct username instead of 'user' or due to the correct account selection. If someoune kows the truth, please comment!
I've been debugging this issue for days, but I can't think of anything else to try to fix this.
The error:
Fatal error: Uncaught exception 'Google_AuthException' with message 'Error refreshing the OAuth2 token, message: '{
"error" : "invalid_grant"
}'' in /html/cms/php/google-api-php-client/src/auth/Google_OAuth2.php:279
Stack trace:
#0 /html/cms/php/google-api-php-client/src/auth/Google_OAuth2.php(256): Google_OAuth2->refreshTokenRequest(Array)
#1 /html/cms/php/google-api-php-client/src/auth/Google_OAuth2.php(209): Google_OAuth2->refreshTokenWithAssertion()
#2 /html/cms/php/google-api-php-client/src/service/Google_ServiceResource.php(166): Google_OAuth2->sign(Object(Google_HttpRequest))
#3 /html/cms/php/google-api-php-client/src/contrib/Google_CalendarService.php(154): Google_ServiceResource->__call('list', Array)
#4 /html/cms/php/google_calendar_sync.php(41): Google_CalendarListServiceResource->listCalendarList()
#5 {main}
thrown in /html/cms/php/google-api-php-client/src/auth/Google_OAuth2.php on line 279
My server's clock is in sync:
2 Jan 13:35:36 ntpdate[1214]: adjust time server 62.212.76.57 offset 0.003544 sec
(62.212.76.57 being my colo company's time server)
I've triple checked my service account developer credentials at the Google APIs Console (I had a screenshot, but I can't use it because my account is too new :/). I've also successfully subscribed to the calendar service (also can't use the screenshot I made of this).
My code:
<?php
require_once(dirname(__FILE__) . '/google-api-php-client/src/Google_Client.php');
require_once(dirname(__FILE__) . '/google-api-php-client/src/contrib/Google_CalendarService.php');
session_start();
define('CLIENT_ID', 'XXXXXX510496.apps.googleusercontent.com');
define('SERVICE_ACCOUNT_NAME', 'XXXXXX510496#developer.gserviceaccount.com');
define('CALENDAR_ID', 'XXXXXXXXXXXXXXXXX');
define('KEY_FILE', dirname(__FILE__) . '/private/XXXXXXXXXXXXXXXXXXXX881a845ed4e4d2d58eb1-privatekey.p12');
$client = new Google_Client();
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
$key = file_get_contents(KEY_FILE);
$client->setClientId(CLIENT_ID);
$client->setAssertionCredentials(new Google_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
'https://www.google.com/calendar/feeds/' . CALENDAR_ID . '/private/full/',
$key)
);
$service = new Google_CalendarService($client);
$calendarList = $service->calendarList->listCalendarList();
echo "<h1>Calendar List</h1><pre>" . print_r($calendarList, true) . "</pre>";
?>
I've completely run out of ideas, so any help would be hugely appreciated!
I just posted a document that might help you get unstuck:
http://www.tqis.com/eloquency/googlecalendar.htm
I'm trying to work with the phpwebsocket library at http://code.google.com/p/phpwebsocket/
I'm using the version r8 of the server.php file. For testing I just tried with the client.html file, also provided by the website.
When the server is started I get this:
Server Started : 2011-08-29 22:11:23
Master socket : Resource id #4
Listening on : www.midomain.com port 12345
But when I load the client.html file in the browser, the server shows the following errors:
Notice: Undefined variable: key1 in /home/mink500/public_html/test/server.php on line 143
Notice: Undefined variable: key2 in /home/mink500/public_html/test/server.php on line 143
Warning: socket_select(): 5 is not a valid Socket resource in /home/mink500/public_html/test/server.php on line 15
There are two variables that are not defined, and the function socket_select() returns the error "5 is not a valid Socket resource"
In the browser I get the "Disconnected" message as soon as the file is loaded.
I tried to make the server work locally using XAMPP (Apache and PHP) but I got the same errors. I also tried to change ports and follow the instructions in this issue:
http://code.google.com/p/phpwebsocket/issues/detail?id=33
But I still get the error "5 is not a valid Socket resource"
I remember that refreshing the page several times I got it running a few months ago, but now it's impossible. Besides, I need it to work all the time, not just after I refresh the page like 20 times.
I also tried with the websocket.class.php file, but this time I get an error on the client side. The browser now returns "Error during WebSocket handshake: 'Sec-WebSocket-Accept' header is missing".
So, I can't make it work with old or new files, with remote or local server, with magic or a ouija board!
Any idea?
Thanks
Starting with the newest phpwebsocket client.html and server.php files I replaced both the getheaders() and dohandshake() functions with the code below works for me in newest Chrome. However it currently does not write to the browser nor stay alive after one uer comment in the chat box.
function dohandshake($user, $buffer) {
$key = null;
console("\nRequesting handshake...");
console($buffer);
console("Handshaking...");
preg_match("#Sec-WebSocket-Key: (.*?)\r\n#", $buffer, $match) && $key = $match[1];
$key .= "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
$key = sha1($key);
$key = pack('H*', $key);
$key = base64_encode($key);
$upgrade =
"HTTP/1.1 101 Switching Protocols\r\n" .
"Upgrade: websocket\r\n" .
"Connection: Upgrade\r\n" .
"Sec-WebSocket-Accept: {$key}\r\n\r\n";
socket_write($user->socket, $upgrade . chr(0), strlen($upgrade . chr(0)));
$user->handshake = true;
console($upgrade);
console("Done handshaking...");
return true;
}
function getheaders($header) {
$retVal = array();
$fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $header));
foreach ($fields as $field) {
if (preg_match('/([^:]+): (.+)/m', $field, $match)) {
$match[1] = preg_replace('/(?<=^|[\x09\x20\x2D])./e', 'strtoupper("\0")', strtolower(trim($match[1])));
if (isset($retVal[$match[1]])) {
$retVal[$match[1]] = array($retVal[$match[1]], $match[2]);
} else {
$retVal[$match[1]] = trim($match[2]);
}
}
}
if (preg_match("/GET (.*) HTTP/", $header, $match)) {
$retVal['GET'] = $match[1];
}
return $retVal;
}
I was testing Zend Gdata 1.10.1 in my localhost.
I downloaded Zend Gdate from this link:
http://framework.zend.com/download/webservices
Inside the Zend Gdata zip file, there was a folder called demos.
I extracted it and used the YouTudeVideoApp to upload a sample video to Youtube.
But every time after I logged into Youtube, before it redirected me to my localhost,
I received a warning message like this warning message:
localhost: This website is registered
with Google to make authorization
requests, but has not been configured
to send requests securely. We
recommend that you continue the
process only if you trust the
following destination:
localhost:8080/youtube/operations.php
So I googled on how to resolve the problem of getting this warning message when
I saw some people suggesed changing the value of $secure to True in operation.php.
Here is the script mentioned:
function generateAuthSubRequestLink($nextUrl = null)
{
$scope = 'http://gdata.youtube.com';
$secure = true;
$session = true;
if (!$nextUrl) {
generateUrlInformation();
$nextUrl = $_SESSION['operationsUrl'];
}
$url = Zend_Gdata_AuthSub::getAuthSubTokenUri($nextUrl, $scope, $secure, $session);
echo '<a href="' . $url
. '"><strong>Click here to authenticate with YouTube</strong></a>';
}
After I altered the value of $secure to True,
I found that the warning message changed to this:
localhost: Registered, secure. This
website is registered with Google to
make authorization requests
The new warning message is somehow shorter and looks better than the previous warning message.
But once I pressed the Allow Access button, it turned out to be this:
ERROR - Token upgrade for
CI3M6_Q3EOGkxoL-_____wEYjffToQQ failed
: Token upgrade failed. Reason:
Invalid AuthSub header. Error 401
ERROR - Unknown search type - ''
I don't know why this happened.
Could you help me solve the problem please?
edit your php.ini file and enable the openssl extensions
extension=php_openssl.dll
restart the httpd or IIS server