0
votes

I'm getting the error of detect intent error, unauthorized 401 in the frontend, I'm using angular and chatbot framework as dialogflow Here in the code im using session as a static value, i don't have idea on the session if that is the issue, please explain to resolve it.

im following this article https://chatbotslife.com/dialogflow-v2-rest-api-communication-6cf7ab66ab36 and replicate it for my own usecase

error:

code: 401
message: "Request had invalid authentication credentials. Expected OAuth 2 access token, login 
cookie or other valid authentication credential. See https://developers.google.com/identity/sign- 
in/web/devconsole-project."
status: "UNAUTHENTICATED"
__proto__: Object

server code

// routings
const express = require("express");
const cors = require("cors");

const app = express();
app.use(cors());
// authentication
const googleAuth = require("google-oauth-jwt");
// accountinfo
const acc = require("./onlineexambot-appenginekey.json");

const getToken = async () => {
console.log("tok");
return new Promise(resolve => {
googleAuth.authenticate(
  {
    email: acc.client_email,
    key: acc.private_key,
    scopes: [
      "https://www.googleapis.com/auth/cloud-platform",
      "https://www.googleapis.com/auth/dialogflow"
    ]
  },
  (err, token) => {
    console.log(err);
    resolve(token);
  }
);
});
};

app.get("/token", async (req, res) => {
let token = await getToken();
console.log("token::", token);
res.send({ token });
});

app.listen(4000, () => {
console.log("listening on 4000");
});

FECode

    import {HttpClient } from '@angular/common/http';
    // import { } from './cookie.service'
    import {environment} from '../environments/environment'
    @Injectable({
    providedIn: 'root'
    })
    export class DfserviceService {
    accessToken:any;
    constructor(private http:HttpClient) { this.getToken()}
    public getToken(){
    this.http.get('http://localhost:4000/token').subscribe((res:any)=>{
      this.accessToken = res.token;
      console.log(res.token)
    });
    }
    public df_client_call(request){
    var config={
      headers:{
        'Authorization':"Bearer"+this.accessToken,
        'Content-Type':'applcation/json;charset=utf-8'
      }
     };
     return this.http.post ('https://dialogflow.googleapis.com/v2/projects/'+
     environment.project_id+'/agent/sessions/qwerty12345'+':detectIntent',request,config);
     }
     }
1
Authorization':"Bearer "+this.accessToken, here is the solution - Nikhil Savaliya
yeah it's working now. - rithesh

1 Answers

1
votes
    'Authorization':"Bearer "+this.accessToken,

Just a minor issue is there, try adding space after Bearer as above and let me know...