OneNoteDev / OneNoteAPISamplePHP

Code sample with examples of how to use the OneNote API on PHP.
Apache License 2.0
36 stars 7 forks source link

submit.php returning blank page #3

Open swbuehler opened 10 years ago

swbuehler commented 10 years ago

I modified the code to iterate through all the PDF files in the same directory to OneNote, but now submit.php is not working (it's giving me a blank page, no errors).

Ideas? The relevant code blocks are the CreatePageWithFile($submittedFile) and the finish() function.

    function createPageWithFile($submittedFile)
    {

        $ch = $this->initCurl();

        //ISO 8601 standard time stamp
        $date = date('c');

        //Read the file into memory
        //Note that reading entire large files into memory could create problems if 
        //  PHP doesn't have enough memory to work with
        $nameofFile = basename($submittedFile);
        $fullFileName = basename($submittedFile) . ".pdf";
        $fileContents = file_get_contents($submittedFile);

        //Includes the  Presentation part and embedded file data part
        //Each has its own Content-Disposition and Content-Type headers
        //The request must end with a blank line to be a valid Multipart request
        $postdata = <<<POSTDATA
--{$this->boundary}
Content-Disposition: form-data; name="Presentation"
Content-Type: text/html

<!DOCTYPE html>
<html>
  <head>
    <title>nameofFile</title>
    <meta name="created" value="$date"/>
  </head>
  <body>
    <object 
        data-attachment="$fullFileName" 
        data="name:$fullFileName" 
        type="application/pdf" />
    <img data-render-src="name:$fullFileName"/>
  </body>
</html>
--{$this->boundary}
Content-Disposition: form-data; name="$fullFileName"
Content-Type: application/pdf

$fileContents
--{$this->boundary}--

POSTDATA;

        curl_setopt($ch,CURLOPT_POSTFIELDS,$postdata);
        $response = curl_exec($ch);
        $this->finish($ch,$response,$submittedFile);
    }

[ . . . ]
    function finish($ch,$response,$submittedFile)
    {

        $info = curl_getinfo($ch);
        curl_close($ch);

        if ($info['http_code'] == 201) 
        {
            unlink($submittedFile);
            echo '<h2>Page created!</h2>';
            $response_without_header = substr($response,$info['header_size']);
            $decoded = json_decode($response_without_header);
            echo 'Open page in <a href="'.
                $decoded->links->oneNoteClientUrl->href.
                    '">OneNote</a> or <a href="'.
                $decoded->links->oneNoteWebUrl->href.
                    '">OneNote Online</a>';
        }
        elseif ($info['http_code'] == 401)
        {
            echo '<h2>Authorization failed. Try signing out and signing in again.</h2>';
        }
        else 
        {
            echo '<h2>Something went wrong...</h2>';
        }
        echo '</b></h2>';

        echo '<h3>Response</h3>';
        echo '<pre>';
        echo htmlspecialchars($response);
        echo '</pre>';
    }
}

[ . . . ]
            case "file":
                foreach (glob("*.[pP][dD][fF]") as $submittedFile {
                    $OneNoteRequest->createPageWithFile($submittedFile); 
                }
                break;
swbuehler commented 10 years ago

OK, the blank page was due to a syntax error, which I fixed, so now I'm just getting this:

OneNote Service API Result

Something went wrong...

Response

<- Go back
garethj-msft commented 10 years ago

Are you still posting once?

Sent from my Windows Phone


From: Steven Buehlermailto:notifications@github.com Sent: ý9/ý13/ý2014 8:16 PM To: OneNoteDev/OneNoteAPISamplePHPmailto:OneNoteAPISamplePHP@noreply.github.com Subject: Re: [OneNoteAPISamplePHP] submit.php returning blank page (#3)

OK, the blank page was due to a syntax error, which I fixed, so now I'm just getting this:

OneNote Service API Result

Something went wrong...

Response

<- Go back

— Reply to this email directly or view it on GitHubhttps://github.com/OneNoteDev/OneNoteAPISamplePHP/issues/3#issuecomment-55514166.

swbuehler commented 10 years ago

I'm trying to run the routine once for each PDF file in the directory. It worked when I had it on the Mac, but when I set it up on IIS on my Windows 8.1 machine I'm getting this response again.

Sent from Windows Mail

machandw-microsoft commented 9 years ago

Can you check the title of the page where you see "Something went wrong" ? Is there an informative message that explains what went wrong ? I am suspecting this relates to a problem with the authentication. When you changed from the Mac to an IIS server, did you update the Root Domain and Redirect URL on the application's registration page to match your new server ? The registration page must reflect correct information for authentication requests from the app to work correctly.

swbuehler commented 9 years ago

I ran it thru Fiddler and what you saw was what I got. No explanation.

The relevant stuff (I know I'm giving away my 'secret' here; I can change that once this issue is resolved).

index.php:

     var client_id = "0000000040137EAE",
        scope = ["wl.signin", "wl.basic", "wl.offline_access", "office.onenote_create"],
        redirect_uri = "http://stevenbuehler.noip.me/OneNote/callback.php";

callback.php:

// Update the following values
define('CLIENTID', '0000000040137EAE');
define('CLIENTSECRET', 'XRzi0ttrCwGll4s3yRLy7SWytmjHlE9X');

// Make sure this is identical to the redirect_uri parameter passed in WL.init() call.
define('CALLBACK', 'http://stevenbuehler.noip.me/OneNote/callback.php');

I have the following set up for the app:

API Settings

Mobile or desktop client app: No Restrict JWT issuing: No Enhanced redirection security: Enabled Root domain: stevenbuehler.noip.me Redirect URLs: http://stevenbuehler.noip.me/OneNote/callback.php App Settings

Client ID: 0000000040137EAE Client secret: XRzi0ttrCwGll4s3yRLy7SWytmjHlE9X

I have stevenbuehler.noip.me mapped to 127.0.0.1 in /etc/hosts and in the hosts file in Windows.

machandw-microsoft commented 9 years ago

Is the URL to your server (and the callback module relative path) reachable by just using the hostname from an external computer ?

This is because the authentication process will try to do that in order to invoke the callback module.

From: Steven Buehler [mailto:notifications@github.com] Sent: Thursday, December 4, 2014 5:08 PM To: OneNoteDev/OneNoteAPISamplePHP Cc: Manoj Chandwani Subject: Re: [OneNoteAPISamplePHP] submit.php returning blank page (#3)

I ran it thru Fiddler and what you saw was what I got. No explanation.

The relevant stuff (I know I'm giving away my 'secret' here; I can change that once this issue is resolved).

index.php:

 var client_id = "0000000040137EAE",

    scope = ["wl.signin", "wl.basic", "wl.offline_access", "office.onenote_create"],

    redirect_uri = "http://stevenbuehler.noip.me/OneNote/callback.php";

submit.php:

// Update the following values

define('CLIENTID', '0000000040137EAE');

define('CLIENTSECRET', 'XRzi0ttrCwGll4s3yRLy7SWytmjHlE9X');

// Make sure this is identical to the redirect_uri parameter passed in WL.init() call.

define('CALLBACK', 'http://stevenbuehler.noip.me/OneNote/callback.php');

I have the following set up for the app:

API Settings

Mobile or desktop client app: No Restrict JWT issuing: No Enhanced redirection security: Enabled Root domain: stevenbuehler.noip.me Redirect URLs: http://stevenbuehler.noip.me/OneNote/callback.php App Settings

Client ID: 0000000040137EAE Client secret: XRzi0ttrCwGll4s3yRLy7SWytmjHlE9X

I have stevenbuehler.noip.me mapped to 127.0.0.1 in /etc/hosts and in the hosts file in Windows.

— Reply to this email directly or view it on GitHubhttps://github.com/OneNoteDev/OneNoteAPISamplePHP/issues/3#issuecomment-65732601.

swbuehler commented 9 years ago

I never had to do that when it was on the Mac. I opened the port in the router and tried again. Same result:

<html><head><title>OneNote Service API Result</title></head><body><h1>OneNote Service API Result</h1><h2>Something went wrong...</h2></b></h2><h3>Response</h3><pre></pre><br /><a href="index.php">&lt;- Go back</a></body></html>
machandw-microsoft commented 9 years ago

Hi Steven,

One last point to check on, can you check on what the line ending convention of your PHP files is ?

I’d recommend using CR LF (the one that applies to Windows).

Thanks,

Manoj

From: Steven Buehler [mailto:notifications@github.com] Sent: Thursday, December 4, 2014 5:18 PM To: OneNoteDev/OneNoteAPISamplePHP Cc: Manoj Chandwani Subject: Re: [OneNoteAPISamplePHP] submit.php returning blank page (#3)

I never had to do that when it was on the Mac. I opened the port in the router and tried again. Same result: OneNote Service API Result OneNote Service API Result Something went wrong... Response

<- Go back

— Reply to this email directly or view it on GitHubhttps://github.com/OneNoteDev/OneNoteAPISamplePHP/issues/3#issuecomment-65733502.

swbuehler commented 9 years ago

I knew about the CRLF issue (in fact I was the one who suggested that note be included in the readme).

machandw-microsoft commented 9 years ago

Hi Steven,

This means that the response to the request is neither a Success (201) nor an Auth issue (401).

Unfortunately, I now need to know what response code is being received.

Could you make a change to increase the verbosity of the error by printing out the response code in the submit.php module :

We wish to change

echo '

Something went wrong...

';

TO

echo '

Something went wrong and the response code is : ' . $info['http_code'] . '

';

Thanks,

Manoj

From: Steven Buehler [mailto:notifications@github.com] Sent: Thursday, December 4, 2014 5:38 PM To: OneNoteDev/OneNoteAPISamplePHP Cc: Manoj Chandwani Subject: Re: [OneNoteAPISamplePHP] submit.php returning blank page (#3)

I knew about the CRLF issue (in fact I was the one who suggested that note be included in the readme).

— Reply to this email directly or view it on GitHubhttps://github.com/OneNoteDev/OneNoteAPISamplePHP/issues/3#issuecomment-65735193.

swbuehler commented 9 years ago

I posted my file versions at http://1drv.ms/122YRwP; maybe someone can tell me what's wrong.

machandw-microsoft commented 9 years ago

I’m looking and there isn’t anything obvious I can find. Could you share the response code you are seeing (either using Fiddler or by adding to the echo) ?

From: Steven Buehler [mailto:notifications@github.com] Sent: Thursday, December 4, 2014 7:26 PM To: OneNoteDev/OneNoteAPISamplePHP Cc: Manoj Chandwani Subject: Re: [OneNoteAPISamplePHP] submit.php returning blank page (#3)

I posted my file versions at http://1drv.ms/122YRwP; maybe someone can tell me what's wrong.

— Reply to this email directly or view it on GitHubhttps://github.com/OneNoteDev/OneNoteAPISamplePHP/issues/3#issuecomment-65742744.

machandw-microsoft commented 9 years ago

I actually cannot find any requests with your application’s identifier on our server’s logs. I am suspecting your requests are not reaching the API server, which could be due to multiple reasons.

Do you have a firewall / similar service enabled that could be blocking requests from going out ?

The response code should also answer if this is the case.

From: Manoj Chandwani Sent: Thursday, December 4, 2014 7:44 PM To: 'OneNoteDev/OneNoteAPISamplePHP'; OneNoteDev/OneNoteAPISamplePHP Subject: RE: [OneNoteAPISamplePHP] submit.php returning blank page (#3)

I’m looking and there isn’t anything obvious I can find. Could you share the response code you are seeing (either using Fiddler or by adding to the echo) ?

From: Steven Buehler [mailto:notifications@github.com] Sent: Thursday, December 4, 2014 7:26 PM To: OneNoteDev/OneNoteAPISamplePHP Cc: Manoj Chandwani Subject: Re: [OneNoteAPISamplePHP] submit.php returning blank page (#3)

I posted my file versions at http://1drv.ms/122YRwP; maybe someone can tell me what's wrong.

— Reply to this email directly or view it on GitHubhttps://github.com/OneNoteDev/OneNoteAPISamplePHP/issues/3#issuecomment-65742744.

swbuehler commented 9 years ago

I pushed the files up to http://stevenwb.azurewebsites.net/onenote and changed the callback URL in the application settings; still the same result (HTTP return code 0).

swbuehler commented 9 years ago

I finally got it working properly on OS X’s default web server without issue, so it must something on my Windows machine’s IIS that’s creating a choke somewhere. I now know my code works, at least. :-)

SB=

machandw-microsoft commented 9 years ago

Great to hear that. I was pretty sure it must be a firewall or a similar agent that was blocking your requests from going out on the IIS.

Hopefully that unblocks you for the time being ☺

Could you check Windows Firewall / an antivirus installation if enabled/applicable for allowing your application/port number.

Thanks, Manoj

From: Steven Buehler [mailto:notifications@github.com] Sent: Friday, December 5, 2014 1:40 PM To: OneNoteDev/OneNoteAPISamplePHP Cc: Manoj Chandwani Subject: Re: [OneNoteAPISamplePHP] submit.php returning blank page (#3)

I finally got it working properly on OS X’s default web server without issue, so it must something on my Windows machine’s IIS that’s creating a choke somewhere. I now know my code works, at least. :-)

SB=

— Reply to this email directly or view it on GitHubhttps://github.com/OneNoteDev/OneNoteAPISamplePHP/issues/3#issuecomment-65858800.

swbuehler commented 9 years ago

Another update: I uploaded the files to Azure Websites (stevenbuehler.azurewebsites.net/onenote), and I'm getting the blank return page again. It says "Something went wrong" with no response string.

PHP error log has the following:

[09-Jan-2015 10:38:15 America/Los_Angeles] PHP Notice:  Undefined index: code in D:\home\site\wwwroot\onenote\callback.php on line 140
[09-Jan-2015 10:38:15 America/Los_Angeles] PHP Notice:  Undefined index: error in D:\home\site\wwwroot\onenote\callback.php on line 176
[09-Jan-2015 10:38:15 America/Los_Angeles] PHP Notice:  Undefined index: error_description in D:\home\site\wwwroot\onenote\callback.php on line 177