Docx file viewer issue

I want to use Aspose.Doc dart for that i set code and API with my client id and client secreate key but i got an error every time : Authantication Failed.

That is your server side error i guess so can you help and please.

Please could you share the exact product you are using?

I am using aspose.cloud words dart and i am facing
This error : Error: Exception: Failed to upload file: {“Message”:“Authentication failed!”}

Please share the code you are using to check what can cause this issue.

i user dart language and i want to do in flutter
i want a Doc file and ppt file viewer in flutter
this is my code :

import ‘dart:convert’;
import ‘dart:io’;
import ‘package:flutter/material.dart’;
import ‘package:http/http.dart’ as http;
import ‘package:path_provider/path_provider.dart’;
import ‘package:flutter/services.dart’;
import ‘package:flutter_pdfview/flutter_pdfview.dart’;

void main() {
runApp(MyApp2());
}

class MyApp2 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text(‘DOC File Viewer’)),
body: Center(
child: ElevatedButton(
onPressed: () async {
final api = AsposeWordsApi();
// Replace with the actual path to your DOCX file in the assets
await api.uploadAndViewDoc(‘assets/sample.docx’, context);
},
child: Text(‘View DOC File’),
),
),
),
);
}
}

class AsposeWordsApi {
final String clientId =
‘399b4908-81fa-4cf0-bace-2c1d5a9bf699’; // Your Client ID
final String clientSecret =
‘6e9c532165878e6bae767419ce02536b’; // Your Client Secret

Future getAccessToken() async {
final response = await http.post(
Uri.parse(‘https://api.aspose.cloud/connect/token’),
headers: {
‘Content-Type’: ‘application/x-www-form-urlencoded’,
},
body: {
‘grant_type’: ‘client_credentials’,
‘client_id’: “399b4908-81fa-4cf0-bace-2c1d5a9bf699”,
‘client_secret’: “6e9c532165878e6bae767419ce02536b”,
},
);

if (response.statusCode == 200) {
  final jsonResponse = json.decode(response.body);
  print(
      'Access Token: ${jsonResponse['access_token']}'); // Log the access token
  return jsonResponse['access_token'];
} else {
  throw Exception('Failed to get access token: ${response.body}');
}

}

Future uploadAndViewDoc(String assetPath, BuildContext context) async {
try {
// Load the DOCX file from assets
final ByteData bytes = await rootBundle.load(assetPath);
final Directory tempDir = await getTemporaryDirectory();
final File file = File(‘${tempDir.path}/sample.docx’);

  // Write the file to the temporary directory
  await file.writeAsBytes(bytes.buffer.asUint8List());

  // Get access token
  final token = await getAccessToken();
  print(
      'Access Token: token $token'); // Log the access token
  

  // Upload the file to Aspose Cloud
  var uploadResponse = await http.put(
    Uri.parse(
        'https://api.aspose.cloud/v3.0/words/storage/file/sample.docx'),
    headers: {
      'Authorization': 'Bearer $token',
      'Content-Type': 'application/octet-stream',
    },
    body: await file.readAsBytes(),
  );

  if (uploadResponse.statusCode != 200) {
    throw Exception('Failed to upload file: ${uploadResponse.body}');
  }

  // Convert the document to PDF
  var convertResponse = await http.put(
    Uri.parse(
        'https://api.aspose.cloud/v3.0/words/sample.docx/convert?format=pdf'),
    headers: {
      'Authorization': 'Bearer $token',
    },
  );

  // Save the PDF file locally
  final pdfFile = File('${tempDir.path}/sample.pdf');
  await pdfFile.writeAsBytes(
      convertResponse.bodyBytes); // Use bodyBytes to get the byte data

  print('Document uploaded and converted to PDF: ${pdfFile.path}');

  // Navigate to PDF Viewer
  Navigator.push(
    context,
    MaterialPageRoute(
      builder: (context) => PDFViewerPage(pdfPath: pdfFile.path),
    ),
  );
} catch (e) {
  print('Error: $e');
}

}
}

class PDFViewerPage extends StatelessWidget {
final String pdfPath;

PDFViewerPage({required this.pdfPath});

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(‘PDF Viewer’)),
body: PDFView(
filePath: pdfPath,
),
);
}
}

Thank you for sharing the code. I will try to replicate the issue.

Okay Thank you for help ,
and Can we connect on call so i can explain you properly

I’m afraid we only provide support here on the forum.

Ok no problam
did you got any solution or it’s helpful if you can connet with me any technical person

Once we come up with a solution, I will reach you.

Hi @Jaimin.fd1 , we suggest you use our SDK package for DART.

Package: aspose_words_cloud | Dart package
Sources: GitHub - aspose-words-cloud/aspose-words-cloud-dart

Our SDK is a REST API wrapper for our cloud service for fast, convenient and typed access to REST API operations without having to thinking about a structure of HTTP requests.

For example, the code for converting a document to a pdf using SDK will look like this:

final creds = Configuration('YOUR_CLIENT_ID', 'YOUR_CLIENT_SECRET');
final api = WordsApi(creds);
final request = ConvertDocumentRequest(requestDocument, 'pdf');
final response = await api.convertDocument(request);

i still can not got it solution.
i want a create doc file viewer in flutter using aspose.doc so can you share me with hole code ?

Your code converts the doc file to a PDF in the sample you shared above, and my colleague’s code does the same but uses SDK, so you don’t need to handle authorization, etc.

Can you share me hole code please

What do you mean by whole code? The shared code is already complete and has been converted to PDF. How you handle PDF is out of the scope of our SDK.

give me doc viewer code which i can use to view document

I can help you only with Aspose Words Cloud API-related questions. If you have any questions regarding this matter, feel free to ask.

I want to use Aspose.Word to Convert Doc file into HTML so how can i do that ?

As we suggested before - please use our Dart SDK aspose_words_cloud | Dart package
aspose-words-cloud-dart/test/document/convert_document_tests.dart at e43079a237c4a4cc3359a6a8dca0353b809e2a33 · aspose-words-cloud/aspose-words-cloud-dart · GitHub here you can find an example of how to convert document, you just need to change ‘pdf’ to ‘HTML’ in the request parameter.